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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
25 * This file contains APIs for OIC Stack to be implemented.
39 /** Macro to use Random port.*/
40 #define USE_RANDOM_PORT (0)
47 * This function Initializes the OC Stack. Must be called prior to starting the stack.
49 * @param mode OCMode Host device is client, server, or client-server.
50 * @param serverFlags OCTransportFlags Default server transport flags.
51 * @param clientFlags OCTransportFlags Default client transport flags.
53 * @return ::OC_STACK_OK on success, some other value upon failure.
55 OC_EXPORT OCStackResult OCInit1(OCMode mode, OCTransportFlags serverFlags, OCTransportFlags clientFlags);
58 * This function Initializes the OC Stack. Must be called prior to starting the stack.
60 * @param ipAddr IP Address of host device. Deprecated parameter.
61 * @param port Port of host device. Deprecated parameter.
62 * @param mode OCMode Host device is client, server, or client-server.
64 * @return ::OC_STACK_OK on success, some other value upon failure.
66 OC_EXPORT OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode);
70 * @brief Set Remote Access information for XMPP Client.
71 * @param raInfo [IN] remote access info.
73 * @return ::OC_STACK_OK on success, some other value upon failure.
75 OCStackResult OCSetRAInfo(const OCRAInfo_t *raInfo);
79 * This function Stops the OC stack. Use for a controlled shutdown.
81 * @note: OCStop() performs operations similar to OCStopPresence(), as well as OCDeleteResource() on
82 * all resources this server is hosting. OCDeleteResource() performs operations similar to
83 * OCNotifyAllObservers() to notify all client observers that the respective resource is being
86 * @return ::OC_STACK_OK on success, some other value upon failure.
88 OC_EXPORT OCStackResult OCStop();
91 * This function starts receiving the multicast traffic. This can be only called
92 * when stack is in OC_STACK_INITIALIZED state but device is not receiving multicast
95 * @return ::OC_STACK_OK on success, some other value upon failure.
97 OCStackResult OCStartMulticastServer();
100 * This function stops receiving the multicast traffic. The rest of the stack
101 * keeps working and no resource are deleted. Device can still receive the unicast
102 * traffic. Once this is set, no response to multicast /oic/res will be sent by the
103 * device. This is to be used for devices that uses other entity to push resources.
105 * @return ::OC_STACK_OK on success, some other value upon failure.
107 OCStackResult OCStopMulticastServer();
110 * This function is Called in main loop of OC client or server.
111 * Allows low-level processing of stack services.
113 * @return ::OC_STACK_OK on success, some other value upon failure.
115 OC_EXPORT OCStackResult OCProcess();
118 * This function discovers or Perform requests on a specified resource
119 * (specified by that Resource's respective URI).
121 * @param handle To refer to the request sent out on behalf of
122 * calling this API. This handle can be used to cancel this operation
123 * via the OCCancel API.
124 * @note: This reference is handled internally, and should not be free'd by
125 * the consumer. A NULL handle is permitted in the event where the caller
126 * has no use for the return value.
127 * @param method To perform on the resource.
128 * @param requestUri URI of the resource to interact with. (Address prefix is deprecated in
129 * favor of destination.)
130 * @param destination Complete description of destination.
131 * @param payload Encoded request payload.
132 * @param connectivityType Modifier flags when destination is not given.
133 * @param qos Quality of service. Note that if this API is called on a uri with the
134 * well-known multicast IP address, the qos will be forced to ::OC_LOW_QOS
135 * since it is impractical to send other QOS levels on such addresses.
136 * @param cbData Asynchronous callback function that is invoked by the stack when
137 * discovery or resource interaction is received. The discovery could be
138 * related to filtered/scoped/particular resource. The callback is
139 * generated for each response received.
140 * @param options The address of an array containing the vendor specific header options
141 * to be sent with the request.
142 * @param numOptions Number of header options to be included.
144 * @note: Presence subscription amendments (i.e. adding additional resource type filters by calling
145 * this API again) require the use of the same base URI as the original request to successfully
146 * amend the presence filters.
148 * @return ::OC_STACK_OK on success, some other value upon failure.
150 OC_EXPORT OCStackResult OCDoResource(OCDoHandle *handle,
152 const char *requestUri,
153 const OCDevAddr *destination,
155 OCConnectivityType connectivityType,
156 OCQualityOfService qos,
157 OCCallbackData *cbData,
158 OCHeaderOption *options,
161 * This function cancels a request associated with a specific @ref OCDoResource invocation.
163 * @param handle Used to identify a specific OCDoResource invocation.
164 * @param qos Used to specify Quality of Service(read below).
165 * @param options Used to specify vendor specific header options when sending
166 * explicit observe cancellation.
167 * @param numOptions Number of header options to be included.
169 * @return ::OC_STACK_OK on success, some other value upon failure.
171 OC_EXPORT OCStackResult OCCancel(OCDoHandle handle,
172 OCQualityOfService qos,
173 OCHeaderOption * options,
177 * Register Persistent storage callback.
178 * @param persistentStorageHandler Pointers to open, read, write, close & unlink handlers.
181 * OC_STACK_OK No errors; Success.
182 * OC_STACK_INVALID_PARAM Invalid parameter.
184 OC_EXPORT OCStackResult OCRegisterPersistentStorageHandler(OCPersistentStorage* persistentStorageHandler);
188 * When operating in OCServer or OCClientServer mode,
189 * this API will start sending out presence notifications to clients via multicast.
190 * Once this API has been called with a success, clients may query for this server's presence and
191 * this server's stack will respond via multicast.
193 * Server can call this function when it comes online for the first time, or when it comes back
194 * online from offline mode, or when it re enters network.
196 * @param ttl Time To Live in seconds.
197 * @note: If ttl is '0', then the default stack value will be used (60 Seconds).
198 * If ttl is greater than ::OC_MAX_PRESENCE_TTL_SECONDS, then the ttl will be
199 * set to ::OC_MAX_PRESENCE_TTL_SECONDS.
201 * @return ::OC_STACK_OK on success, some other value upon failure.
203 OC_EXPORT OCStackResult OCStartPresence(const uint32_t ttl);
206 * When operating in OCServer or OCClientServer mode, this API will stop sending
207 * out presence notifications to clients via multicast.
208 * Once this API has been called with a success this server's stack will not respond to clients
209 * querying for this server's presence.
211 * Server can call this function when it is terminating, going offline, or when going
214 * @return ::OC_STACK_OK on success, some other value upon failure.
217 OC_EXPORT OCStackResult OCStopPresence();
222 * This function sets default device entity handler.
224 * @param entityHandler Entity handler function that is called by ocstack to handle requests
225 * for any undefined resources or default actions.If NULL is passed it
226 * removes the device default entity handler.
227 * @param callbackParameter Parameter passed back when entityHandler is called.
229 * @return ::OC_STACK_OK on success, some other value upon failure.
231 OC_EXPORT OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandler, void* callbackParameter);
234 * This function sets device information.
236 * Upon call to OCInit, the default Device Type (i.e. "rt") has already been set to the default
237 * Device Type "oic.wk.d". You do not have to specify "oic.wk.d" in the OCDeviceInfo.types linked
238 * list. The default Device Type is mandatory and always specified by this Device as the first
241 * @param deviceInfo Structure passed by the server application containing the device
245 * ::OC_STACK_OK no errors.
246 * ::OC_STACK_INVALID_PARAM invalid parameter.
247 * ::OC_STACK_ERROR stack process error.
249 OC_EXPORT OCStackResult OCSetDeviceInfo(OCDeviceInfo deviceInfo);
252 * This function sets platform information.
254 * @param platformInfo Structure passed by the server application containing
255 * the platform information.
259 * ::OC_STACK_OK no errors.
260 * ::OC_STACK_INVALID_PARAM invalid parameter.
261 * ::OC_STACK_ERROR stack process error.
263 OC_EXPORT OCStackResult OCSetPlatformInfo(OCPlatformInfo platformInfo);
266 * This function creates a resource.
268 * @param handle Pointer to handle to newly created resource. Set by ocstack and
269 * used to refer to resource.
270 * @param resourceTypeName Name of resource type. Example: "core.led".
271 * @param resourceInterfaceName Name of resource interface. Example: "core.rw".
272 * @param uri URI of the resource. Example: "/a/led".
273 * @param entityHandler Entity handler function that is called by ocstack to handle
275 * NULL for default entity handler.
276 * @param callbackParam parameter passed back when entityHandler is called.
277 * @param resourceProperties Properties supported by resource.
278 * Example: ::OC_DISCOVERABLE|::OC_OBSERVABLE.
280 * @return ::OC_STACK_OK on success, some other value upon failure.
282 OC_EXPORT OCStackResult OCCreateResource(OCResourceHandle *handle,
283 const char *resourceTypeName,
284 const char *resourceInterfaceName,
286 OCEntityHandler entityHandler,
288 uint8_t resourceProperties);
292 * This function adds a resource to a collection resource.
294 * @param collectionHandle Handle to the collection resource.
295 * @param resourceHandle Handle to resource to be added to the collection resource.
297 * @return ::OC_STACK_OK on success, some other value upon failure.
299 OC_EXPORT OCStackResult OCBindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle);
302 * This function removes a resource from a collection resource.
304 * @param collectionHandle Handle to the collection resource.
305 * @param resourceHandle Handle to resource to be removed from the collection resource.
307 * @return ::OC_STACK_OK on success, some other value upon failure.
309 OC_EXPORT OCStackResult OCUnBindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle);
312 * This function binds a resource type to a resource.
314 * @param handle Handle to the resource.
315 * @param resourceTypeName Name of resource type. Example: "core.led".
317 * @return ::OC_STACK_OK on success, some other value upon failure.
319 OC_EXPORT OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
320 const char *resourceTypeName);
322 * This function binds a resource interface to a resource.
324 * @param handle Handle to the resource.
325 * @param resourceInterfaceName Name of resource interface. Example: "core.rw".
327 * @return ::OC_STACK_OK on success, some other value upon failure.
329 OC_EXPORT OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
330 const char *resourceInterfaceName);
333 * This function binds an entity handler to the resource.
335 * @param handle Handle to the resource that the contained resource is to be bound.
336 * @param entityHandler Entity handler function that is called by ocstack to handle requests.
337 * @param callbackParameter Context parameter that will be passed to entityHandler.
339 * @return ::OC_STACK_OK on success, some other value upon failure.
341 OC_EXPORT OCStackResult OCBindResourceHandler(OCResourceHandle handle,
342 OCEntityHandler entityHandler,
343 void *callbackParameter);
346 * This function gets the number of resources that have been created in the stack.
348 * @param numResources Pointer to count variable.
350 * @return ::OC_STACK_OK on success, some other value upon failure.
352 OC_EXPORT OCStackResult OCGetNumberOfResources(uint8_t *numResources);
355 * This function gets a resource handle by index.
357 * @param index Index of resource, 0 to Count - 1.
359 * @return Found resource handle or NULL if not found.
361 OC_EXPORT OCResourceHandle OCGetResourceHandle(uint8_t index);
364 * This function deletes resource specified by handle. Deletes resource and all
365 * resource type and resource interface linked lists.
367 * @note: OCDeleteResource() performs operations similar to OCNotifyAllObservers() to notify all
368 * client observers that "this" resource is being deleted.
370 * @param handle Handle of resource to be deleted.
372 * @return ::OC_STACK_OK on success, some other value upon failure.
374 OC_EXPORT OCStackResult OCDeleteResource(OCResourceHandle handle);
377 * Get a string representation the server instance ID.
378 * The memory is managed internal to this function, so freeing externally will result
379 * in a runtime error.
380 * Note: This will NOT seed the RNG, so it must be called after the RNG is seeded.
381 * This is done automatically during the OCInit process,
382 * so ensure that this call is done after that.
384 * @return A string representation the server instance ID.
386 OC_EXPORT const char* OCGetServerInstanceIDString(void);
389 * This function gets the URI of the resource specified by handle.
391 * @param handle Handle of resource.
393 * @return URI string if resource found or NULL if not found.
395 OC_EXPORT const char *OCGetResourceUri(OCResourceHandle handle);
398 * This function gets the properties of the resource specified by handle.
400 * @param handle Handle of resource.
402 * @return OCResourceProperty Bitmask or -1 if resource is not found.
404 * @note that after a resource is created, the OC_ACTIVE property is set for the resource by the
407 OC_EXPORT OCResourceProperty OCGetResourceProperties(OCResourceHandle handle);
410 * This function gets the number of resource types of the resource.
412 * @param handle Handle of resource.
413 * @param numResourceTypes Pointer to count variable.
415 * @return ::OC_STACK_OK on success, some other value upon failure.
417 OC_EXPORT OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle, uint8_t *numResourceTypes);
420 * This function gets name of resource type of the resource.
422 * @param handle Handle of resource.
423 * @param index Index of resource, 0 to Count - 1.
425 * @return Resource type name if resource found or NULL if resource not found.
427 OC_EXPORT const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index);
430 * This function gets the number of resource interfaces of the resource.
432 * @param handle Handle of resource.
433 * @param numResourceInterfaces Pointer to count variable.
435 * @return ::OC_STACK_OK on success, some other value upon failure.
437 OC_EXPORT OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle,
438 uint8_t *numResourceInterfaces);
441 * This function gets name of resource interface of the resource.
443 * @param handle Handle of resource.
444 * @param index Index of resource, 0 to Count - 1.
446 * @return Resource interface name if resource found or NULL if resource not found.
448 OC_EXPORT const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index);
451 * This function gets methods of resource interface of the resource.
453 * @param handle Handle of resource.
454 * @param index Index of resource, 0 to Count - 1.
456 * @return Allowed methods if resource found or NULL if resource not found.
458 OC_EXPORT uint8_t OCGetResourceInterfaceAllowedMethods(OCResourceHandle handle, uint8_t index);
461 * This function gets resource handle from the collection resource by index.
463 * @param collectionHandle Handle of collection resource.
464 * @param index Index of contained resource, 0 to Count - 1.
466 * @return Handle to contained resource if resource found or NULL if resource not found.
468 OC_EXPORT OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle,
472 * This function gets the entity handler for a resource.
474 * @param handle Handle of resource.
476 * @return Entity handler if resource found or NULL resource not found.
478 OC_EXPORT OCEntityHandler OCGetResourceHandler(OCResourceHandle handle);
481 * This function notify all registered observers that the resource representation has
482 * changed. If observation includes a query the client is notified only if the query is valid after
483 * the resource representation has changed.
485 * @param handle Handle of resource.
486 * @param qos Desired quality of service for the observation notifications.
488 * @return ::OC_STACK_OK on success, some other value upon failure.
490 OC_EXPORT OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService qos);
493 * Notify specific observers with updated value of representation.
494 * Before this API is invoked by entity handler it has finished processing
495 * queries for the associated observers.
497 * @param handle Handle of resource.
498 * @param obsIdList List of observation IDs that need to be notified.
499 * @param numberOfIds Number of observation IDs included in obsIdList.
500 * @param payload Object representing the notification
501 * @param qos Desired quality of service of the observation notifications.
503 * @note: The memory for obsIdList and payload is managed by the entity invoking the API.
504 * The maximum size of the notification is 1015 bytes for non-Arduino platforms. For Arduino
505 * the maximum size is 247 bytes.
507 * @return ::OC_STACK_OK on success, some other value upon failure.
509 OC_EXPORT OCStackResult
510 OCNotifyListOfObservers (OCResourceHandle handle,
511 OCObservationId *obsIdList,
513 const OCRepPayload *payload,
514 OCQualityOfService qos);
518 * This function sends a response to a request.
519 * The response can be a normal, slow, or block (i.e. a response that
520 * is too large to be sent in a single PDU and must span multiple transmissions).
522 * @param response Pointer to structure that contains response parameters.
524 * @return ::OC_STACK_OK on success, some other value upon failure.
526 OC_EXPORT OCStackResult OCDoResponse(OCEntityHandlerResponse *response);
528 //#ifdef DIRECT_PAIRING
530 * The function is responsible for discovery of direct-pairing device is current subnet. It will list
531 * all the device in subnet which support direct-pairing.
532 * Caller must NOT free returned constant pointer
534 * @param[in] timeout Timeout in seconds, value till which function will listen to responses from
535 * client before returning the list of devices.
536 * @return OCDirectPairingDev_t pointer in case of success and NULL otherwise.
538 OC_EXPORT const OCDPDev_t* OCDiscoverDirectPairingDevices(unsigned short waittime);
541 * The function is responsible for return of paired device list via direct-pairing. It will list
542 * all the device which is previousely paired with client.
543 * Caller must NOT free returned constant pointer
545 * @return OCDirectPairingDev_t pointer in case of success and NULL otherwise.
547 OC_EXPORT const OCDPDev_t* OCGetDirectPairedDevices();
550 * The function is responsible for establishment of direct-pairing. It will proceed mode negotiation
551 * and connect PIN based dtls session.
553 * @param[in] peer Target device to establish direct-pairing.
554 * @param[in] pmSel Selected mode of pairing.
555 * @param[in] pinNumber PIN number for authentication, pin lenght is defined DP_PIN_LENGTH(8).
556 * @param[in] resultCallback Callback fucntion to event status of process.
557 * @return OTM_SUCCESS in case of success and other value otherwise.
559 OC_EXPORT OCStackResult OCDoDirectPairing(void *ctx, OCDPDev_t* peer, OCPrm_t pmSel, char *pinNumber,
560 OCDirectPairingCB resultCallback);
561 //#endif // DIRECT_PAIRING
565 #endif // __cplusplus
567 #endif /* OCSTACK_H_ */