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