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