Add a maximum presence TTL of 24 hours
[platform/upstream/iotivity.git] / resource / include / OCPlatform.h
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 /**
22  * @file
23  *
24  * This file contains the declaration of classes and its members related to
25  * OCPlatform.
26  */
27
28 #ifndef __OCPLATFORM_H
29 #define __OCPLATFORM_H
30 #include <OCApi.h>
31 #include <OCPlatform_impl.h>
32 namespace OC
33 {
34     /**
35     * @brief: This namespace contains the main entrance/functionality of the product.
36     * It may be used with OC::OCPlatform::functionName.  To set a custom configuration,
37     * the implementer must make a call to OCPlatform::Configure before the first usage
38     * of a function in this namespace.
39     */
40     namespace OCPlatform
41     {
42         /**
43         * API for overwriting the default configuration of the OCPlatform object.
44         * Note: Any calls made to this AFTER the first call to OCPlatform::Instance
45         * will have no affect
46         */
47         void Configure(const PlatformConfig& config);
48
49         // typedef for handle to cancel presence info with
50         typedef OCDoHandle OCPresenceHandle;
51
52         /**
53         * API for notifying base that resource's attributes have changed.
54         *
55         * @param OCResourceHandle resource handle of the resource
56         * @param QualityOfService the quality of communication
57         *
58         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
59         * NOTE: This API is for server side only.
60         * NOTE: OCResourceHandle is defined in ocstack.h.
61         * NOTE: OCStackResult is defined in ocstack.h.
62         */
63         OCStackResult notifyAllObservers(OCResourceHandle resourceHandle);
64         OCStackResult notifyAllObservers(OCResourceHandle resourceHandle, QualityOfService QoS);
65
66         /**
67         * API for notifying only specific clients that resource's attributes have changed.
68         *
69         * @param OCResourceHandle resource handle of the resource
70         * @param observationIds std vector of observationIds. These set of ids are ones which
71         * which will be notified upon resource change.
72         * @param responsePtr OCResourceResponse pointer used by app to fill the response for this
73         * resource change.
74         * @param QualityOfService the quality of communication
75         *
76         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
77         *
78         * NOTE: This API is for server side only.
79         * NOTE: OCResourceHandle is defined in ocstack.h.
80         * NOTE: OCStackResult is defined in ocstack.h.
81         */
82         OCStackResult notifyListOfObservers(
83                     OCResourceHandle resourceHandle,
84                     ObservationIds& observationIds,
85                     const std::shared_ptr<OCResourceResponse> responsePtr);
86         OCStackResult notifyListOfObservers(
87                     OCResourceHandle resourceHandle,
88                     ObservationIds& observationIds,
89                     const std::shared_ptr<OCResourceResponse> responsePtr,
90                     QualityOfService QoS);
91
92         /**
93         * API for Service and Resource Discovery.
94         * NOTE: This API applies to client side only.
95         *
96         * @param host - Host IP Address of a service to direct resource discovery query. If null or
97         *        empty, performs multicast resource discovery query
98         * @param resourceURI - name of the resource. If null or empty, performs search for all
99         *       resource names
100         * @param connectivityType - @ref OCConnectivityType type of connectivity indicating the
101         *                           interface. Example: OC_WIFI, OC_ETHERNET, OC_ALL
102         * @param handler - Handles callbacks, success states and failure states.
103         *
104         *        Four modes of discovery defined as follows:
105         *        (NULL/Empty, NULL/Empty) - Performs ALL service discovery AND ALL resource
106         *           discovery.
107         *        (NULL/Empty, Not Empty) - Performs query for a filtered/scoped/particular
108         *                                   resource(s) from ALL services.
109         *        (Not Empty, NULL/Empty) - Performs ALL resource discovery on a particular service.
110         *        (Not Empty, Not Empty) - Performs query for a filtered/scoped/particular
111         *                                   resource(s)
112         *                                  from a particular service.
113         * @param QualityOfService the quality of communication
114         *
115         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
116         * NOTE: First parameter 'host' currently represents an IP address. This will change in
117         * future and will refer to endpoint interface so that we can refer to other transports such
118         * as BTH etc.
119         * NOTE: OCStackResult is defined in ocstack.h.
120         */
121         OCStackResult findResource(const std::string& host, const std::string& resourceURI,
122                     OCConnectivityType connectivityType, FindCallback resourceHandler);
123         OCStackResult findResource(const std::string& host, const std::string& resourceURI,
124                     OCConnectivityType connectivityType, FindCallback resourceHandler,
125                     QualityOfService QoS);
126
127         /**
128         * API for Device Discovery
129         *
130         *
131         * @param host - Host IP Address. If null or empty, Multicast is performed.
132         * @param deviceURI - Uri containing address to the virtual device in C Stack
133                                 ("/oc/core/d")
134         * @param connectivityType - @ref OCConnectivityType type of connectivity indicating the
135         *                           interface. Example: OC_WIFI, OC_ETHERNET, OC_ALL
136         * @param deviceInfoHandler - device discovery callback
137         * @param QoS the quality of communication
138         *
139         */
140         OCStackResult getDeviceInfo(const std::string& host, const std::string& deviceURI,
141                     OCConnectivityType connectivityType, FindDeviceCallback deviceInfoHandler);
142         OCStackResult getDeviceInfo(const std::string& host, const std::string& deviceURI,
143                     OCConnectivityType connectivityType, FindDeviceCallback deviceInfoHandler,
144                     QualityOfService QoS);
145
146         /**
147         * This API registers a resource with the server
148         * NOTE: This API applies to server side only.
149         *
150         * @param resourceHandle - Upon successful registration, resourceHandle will be filled
151         * @param resourceURI - The URI of the resource. Example: "a/light". See NOTE below
152         * @param resourceTypeName - The resource type. Example: "light"
153         * @param resourceInterface - The resource interface (whether it is collection etc).
154         * @param entityHandler - entity handler callback.
155         * @param resourceProperty - indicates the property of the resource. Defined in ocstack.h.
156         * setting resourceProperty as OC_DISCOVERABLE will allow Discovery of this resource
157         * setting resourceProperty as OC_OBSERVABLE will allow observation
158         * settings resourceProperty as OC_DISCOVERABLE | OC_OBSERVABLE will allow both discovery and
159         * observation
160         *
161         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
162         * NOTE: "a/light" is a relative URI.
163         * Above relative URI will be prepended (by core) with a host IP + namespace "oc"
164         * Therefore, fully qualified URI format would be //HostIP-Address/namespace/relativeURI"
165         * Example, a relative URI: 'a/light' will result in a fully qualified URI:
166         *   //192.168.1.1/oc/a/light"
167         * First parameter can take a relative URI and core will take care of preparing the fully
168         * qualified URI OR
169         * first parameter can take fully qualified URI and core will take that as is for further
170         * operations
171         * NOTE: OCStackResult is defined in ocstack.h.
172         */
173         OCStackResult registerResource(OCResourceHandle& resourceHandle,
174                         std::string& resourceURI,
175                         const std::string& resourceTypeName,
176                         const std::string& resourceInterface,
177                         EntityHandler entityHandler,
178                         uint8_t resourceProperty);
179
180         /**
181         * This API registers a resource with the server
182         * NOTE: This API applies to server & client side.
183
184         * @param resourceHandle - Upon successful registration, resourceHandle will be filled
185         * @param OCResource - The instance of OCResource that all data filled.
186         *
187         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
188         */
189         OCStackResult registerResource(OCResourceHandle& resourceHandle,
190                         const std::shared_ptr< OCResource > resource);
191
192         /**
193         * Register Device Info
194         *
195         * @param deviceInfo - structure containing all the device specific information
196         *
197         * @return
198         *      OC_STACK_OK   - no errors
199         *      OC_STACK_ERROR - stack process error
200         */
201         OCStackResult registerDeviceInfo(const OCDeviceInfo deviceInfo);
202
203         /**
204         * Set default device entity handler
205         *
206         * @param entityHandler - entity handler to handle requests for
207         *                        any undefined resources or default actions.
208         *                        if NULL is passed it removes the device default entity handler.
209         *
210         * @return
211         *     OC_STACK_OK    - no errors
212         *     OC_STACK_ERROR - stack process error
213         */
214         OCStackResult setDefaultDeviceEntityHandler(EntityHandler entityHandler);
215
216         /**
217         * This API unregisters a resource with the server
218         * NOTE: This API applies to server side only.
219         *
220         * @param resourceHandle - This is the resource handle which we which to unregister from the
221         * server
222         *
223         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
224         * NOTE: OCStackResult is defined in ocstack.h.
225         */
226         OCStackResult unregisterResource(const OCResourceHandle& resourceHandle);
227
228         /**
229         * Add a resource to a collection resource.
230         *
231         * @param collectionHandle - handle to the collection resource
232         * @param addedResourceHandle - handle to resource to be added to the collection resource
233         *
234         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.<br>
235         * NOTE: OCStackResult is defined in ocstack.h. <br>
236         * NOTE: bindResource must be used only after the both collection resource and
237         * resource to add under a collections are created and respective handles obtained<br>
238         * <b>Example:</b> <br>
239         * Step 1: registerResource(homeResourceHandle, "a/home", "home", Link_Interface,
240         *   entityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
241         * Step 2: registerResource(kitchenResourceHandle, "a/kitchen", "kitchen", Link_Interface,
242         *   entityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
243         * Step 3: bindResource(homeResourceHandle, kitchenResourceHandle);<br>
244         * At the end of Step 3, resource "a/home" will contain a reference to "a/kitchen".<br>
245         */
246         OCStackResult bindResource(const OCResourceHandle collectionHandle,
247                 const OCResourceHandle resourceHandle);
248
249         /**
250         * Add multiple resources to a collection resource.
251         *
252         * @param collectionHandle - handle to the collection resource
253         * @param addedResourceHandleList reference to list of resource handles to be added to the
254         *   collection resource
255         *
256         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. <br>
257         * NOTE: OCStackResult is defined in ocstack.h. <br>
258         * NOTE: bindResources must be used only after the both collection resource and
259         * list of resources to add under a collection are created and respective handles
260         * obtained <br>
261         * <b> Example: </b> <br>
262         * Step 1: registerResource(homeResourceHandle, "a/home", "home", Link_Interface,
263         *   homeEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
264         * Step 2: registerResource(kitchenResourceHandle, "a/kitchen", "kitchen", Link_Interface,
265         *   kitchenEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
266         * Step 3: registerResource(roomResourceHandle, "a/room", "room", Link_Interface,
267         *   roomEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
268         * Step 4: std::vector<OCResourceHandle> rList; rList.push_back(kitchenResourceHandle);
269         *   rList.push_back(roomResourceHandle);<br>
270         * Step 5: bindResource(homeResourceHandle, rList);<br>
271         * At the end of Step 5, resource "a/home" will contain a references to "a/kitchen" and
272         *   "a/room" <br>
273         */
274         OCStackResult bindResources(const OCResourceHandle collectionHandle,
275                 const std::vector<OCResourceHandle>& addedResourceHandleList);
276
277         /**
278         * Unbind a resource from a collection resource.
279         *
280         * @param collectionHandle - handle to the collection resource
281         * @param resourceHandle resource handle to be unbound from the collection resource
282         *
283         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. <br>
284         * NOTE: OCStackResult is defined in ocstack.h.<br>
285         * NOTE: unbindResource must be used only after the both collection resource and
286         * resource to unbind from a collection are created and respective handles obtained<br>
287         * <b> Example </b> <br>
288         * Step 1: registerResource(homeResourceHandle, "a/home", "home", Link_Interface,
289         *   entityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
290         * Step 2: registerResource(kitchenResourceHandle, "a/kitchen", "kitchen", Link_Interface,
291         *   entityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
292         * Step 3: bindResource(homeResourceHandle, kitchenResourceHandle);<br>
293         * Step 4: unbindResource(homeResourceHandle, kitchenResourceHandle);<br>
294         * At the end of Step 4, resource "a/home" will no longer reference "a/kitchen". <br>
295         */
296         OCStackResult unbindResource(const OCResourceHandle collectionHandle,
297                     const OCResourceHandle resourceHandle);
298
299         /**
300         * Unbind resources from a collection resource.
301         *
302         * @param collectionHandle - handle to the collection resource
303         * @param resourceHandleList List of resource handles to be unbound from the collection
304         *   resource
305         *
306         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. <br>
307         *
308         * NOTE: OCStackResult is defined in ocstack.h.<br>
309         * NOTE: unbindResources must be used only after the both collection resource and
310         * list of resources resource to unbind from a collection are created and respective handles
311         *   obtained. <br>
312         * <b>Example</b> <br>
313         * Step 1: registerResource(homeResourceHandle, "a/home", "home", Link_Interface,
314         *   homeEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
315         * Step 2: registerResource(kitchenResourceHandle, "a/kitchen", "kitchen", Link_Interface,
316         *   kitchenEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
317         * Step 3: registerResource(roomResourceHandle, "a/room", "room", Link_Interface,
318         *   roomEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
319         * Step 4: std::vector<OCResourceHandle> rList; rList.push_back(kitchenResourceHandle);
320         *   rList.push_back(roomResourceHandle);<br>
321         * Step 5: bindResource(homeResourceHandle, rList);<br>
322         * Step 6: unbindResources(homeResourceHandle, rList);<br>
323         * At the end of Step 6, resource "a/home" will no longer reference to "a/kitchen" and
324         *   "a/room"<br>
325         */
326         OCStackResult unbindResources(const OCResourceHandle collectionHandle,
327                         const std::vector<OCResourceHandle>& resourceHandleList);
328
329         /**
330         * Binds a type to a particular resource
331         * @param resourceHandle - handle to the resource
332         * @param resourceTypeName - new typename to bind to the resource
333
334         * @return OCStackResult - return value of the API. Returns OCSTACK_OK if success <br>
335         */
336         OCStackResult bindTypeToResource(const OCResourceHandle& resourceHandle,
337                         const std::string& resourceTypeName);
338
339         /**
340         * Binds an interface to a particular resource
341         * @param resourceHandle - handle to the resource
342         * @param resourceTypeName - new interface  to bind to the resource
343
344         * @return OCStackResult - return value of the API. Returns OCSTACK_OK if success <br>
345         */
346         OCStackResult bindInterfaceToResource(const OCResourceHandle& resourceHandle,
347                         const std::string& resourceInterfaceName);
348
349
350         /**
351         * Start Presence announcements.
352         *
353         * @param ttl - time to live
354         * Note: If ttl is '0', then the default stack value will be used (60 Seconds).
355         *
356         *       If ttl is greater than OC_MAX_PRESENCE_TTL_SECONDS, then the ttl will be set to
357         *       OC_MAX_PRESENCE_TTL_SECONDS.
358         *
359         * @return OCStackResult - Returns OCSTACK_OK if success <br>
360         *
361         * Server can call this function when it comes online for the
362         * first time, or when it comes back online from offline mode,
363         * or when it re enters network.
364         *
365         */
366         OCStackResult startPresence(const unsigned int ttl);
367
368         /**
369         * Stop Presence announcements.
370         *
371         * @return OCStackResult - Returns OCSTACK_OK if success <br>
372         *
373         * Server can call this function when it is terminating,
374         * going offline, or when going away from network.
375         *
376         */
377         OCStackResult stopPresence();
378
379         /**
380         * subscribes to a server's presence change events.  By making this subscription,
381         * every time a server adds/removes/alters a resource, starts or is intentionally
382         * stopped (potentially more to be added later).
383         *
384         * @param presenceHandle - a handle object that can be used to identify this subscription
385         *               request.  It can be used to unsubscribe from these events in the future.
386         *               It will be set upon successful return of this method.
387         * @param host - The IP address/addressable name of the server to subscribe to.
388         *               This should be in the format coap://address:port
389         * @param connectivityType - @ref OCConnectivityType type of connectivity indicating the
390         *                           interface. Example: OC_WIFI, OC_ETHERNET, OC_ALL
391         * @param resourceType - a resource type specified as a filter for subscription callbacks.
392         * @param presenceHandler - callback function that will receive notifications/subscription
393         *               events
394         *
395         * @return OCStackResult - return value of the API.  Returns OCSTACK_OK if success <br>
396         */
397         OCStackResult subscribePresence(OCPresenceHandle& presenceHandle, const std::string& host,
398                         OCConnectivityType connectivityType, SubscribeCallback presenceHandler);
399         OCStackResult subscribePresence(OCPresenceHandle& presenceHandle, const std::string& host,
400                         const std::string& resourceType, OCConnectivityType connectivityType,
401                         SubscribeCallback presenceHandler);
402
403         /**
404         * unsubscribes from a previously subscribed server's presence events. Note that
405         * you may for a short time still receive events from the server since it may take time
406         * for the unsubscribe to take effect.
407         *
408         * @param presenceHandle - the handle object provided by the subscribePresence call that
409         *               identifies this subscription.
410         *
411         * @return OCStackResult - return value of the API.  Returns OCSTACK_OK if success <br>
412         */
413         OCStackResult unsubscribePresence(OCPresenceHandle presenceHandle);
414
415         /**
416         * Creates a resource proxy object so that get/put/observe functionality
417         * can be used without discovering the object in advance.  Note that the
418         * consumer of this method needs to provide all of the details required to
419         * correctly contact and observe the object. If the consumer lacks any of
420         * this information, they should discover the resource object normally.
421         * Additionally, you can only create this object if OCPlatform was initialized
422         * to be a Client or Client/Server.  Otherwise, this will return an empty
423         * shared ptr.
424         *
425         * @param host - a string containing a resolvable host address of the server
426         *           holding the resource. Currently this should be in the format
427         *           coap://address:port, though in the future, we expect this to
428         *           change to //address:port
429         *
430         * @param uri - the rest of the resource's URI that will permit messages to be
431         *           properly routed.  Example: /a/light
432         *
433         * @param connectivityType - @ref OCConnectivityType type of connectivity indicating the
434         *                           interface. Example: OC_WIFI, OC_ETHERNET, OC_ALL
435         *
436         * @param isObservable - a boolean containing whether the resource supports observation
437         *
438         * @param resourceTypes - a collection of resource types implemented by the resource
439         *
440         * @param interfaces - a collection of interfaces that the resource supports/implements
441         * @return OCResource::Ptr - a shared pointer to the new resource object
442         */
443         OCResource::Ptr constructResourceObject(const std::string& host, const std::string& uri,
444                         OCConnectivityType connectivityType, bool isObservable,
445                         const std::vector<std::string>& resourceTypes,
446                         const std::vector<std::string>& interfaces);
447
448         /**
449         * Allows application entity handler to send response to an incoming request.
450         *
451         * @param pResponse - OCResourceResponse pointer that will permit to set values related
452         * to resource response. <br>
453         * @return OCStackResult - return value of the API. Returns OCSTACK_OK if success <br>
454         */
455         OCStackResult sendResponse(const std::shared_ptr<OCResourceResponse> pResponse);
456     }
457 }
458
459 #endif //__OCPLATFORM_H
460
461
462