Merge "Add scons script and things manager samples."
[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 /// @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          * API for Device Discovery
150          *
151          * @param host - Host IP Address. If null or empty, Multicast is performed.
152          * @param resourceURI - Uri containing address to the virtual device in C Stack
153          *                       ("/oc/core/d")
154          *
155          * @param QualityOfService the quality of communication
156          *
157          */
158         OCStackResult getDeviceInfo(const std::string& host, const std::string& deviceURI,
159                     FindDeviceCallback deviceInfoHandler);
160         OCStackResult getDeviceInfo(const std::string& host, const std::string& deviceURI,
161                     FindDeviceCallback deviceInfoHandler, QualityOfService QoS);
162
163         /**
164         * This API registers a resource with the server
165         * NOTE: This API applies to server side only.
166         *
167         * @param resourceHandle - Upon successful registration, resourceHandle will be filled
168         * @param resourceURI - The URI of the resource. Example: "a/light". See NOTE below
169         * @param resourceTypeName - The resource type. Example: "light"
170         * @param resourceInterface - The resource interface (whether it is collection etc).
171         * @param entityHandler - entity handler callback.
172         * @param resourceProperty - indicates the property of the resource. Defined in ocstack.h.
173         * setting resourceProperty as OC_DISCOVERABLE will allow Discovery of this resource
174         * setting resourceProperty as OC_OBSERVABLE will allow observation
175         * settings resourceProperty as OC_DISCOVERABLE | OC_OBSERVABLE will allow both discovery
176         * and observation
177         *
178         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
179         * NOTE: "a/light" is a relative URI.
180         * Above relative URI will be prepended (by core) with a host IP + namespace "oc"
181         * Therefore, fully qualified URI format would be //HostIP-Address/namespace/relativeURI"
182         * Example, a relative URI: 'a/light' will result in a fully qualified URI:
183         *   //192.168.1.1/oc/a/light"
184         * First parameter can take a relative URI and core will take care of preparing the fully
185         * qualified URI OR
186         * first paramter can take fully qualified URI and core will take that as is for further
187         * operations
188         * NOTE: OCStackResult is defined in ocstack.h.
189         */
190         OCStackResult registerResource(OCResourceHandle& resourceHandle,
191                         std::string& resourceURI,
192                         const std::string& resourceTypeName,
193                         const std::string& resourceInterface,
194                         EntityHandler entityHandler,
195                         uint8_t resourceProperty);
196
197         /**
198          * This API registers a resource with the server
199          * NOTE: This API applies to server & client side.
200
201          * @param resourceHandle - Upon successful registration, resourceHandle will be filled
202          * @param OCResource - The instance of OCResource that all data filled.
203          *
204          * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
205          */
206
207         OCStackResult registerResource(OCResourceHandle& resourceHandle,
208                         const std::shared_ptr<OCResource> resource);
209
210         /**
211          * This API registers all the device specific information
212          *
213          * @param OCDeviceInfo - Structure containing all the device related information
214          *
215          * @return OCStackResult return value of the API. Returns OC_STACK_OK if success
216          *
217          * Note: OCDeviceInfo is defined in OCStack.h
218          */
219         OCStackResult registerDeviceInfo(const OCDeviceInfo deviceInfo);
220
221         /**
222         * Set default device entity handler
223         *
224         * @param entityHandler - entity handler to handle requests for
225         *                        any undefined resources or default actions.
226         *                        if NULL is passed it removes the device default entity handler.
227         *
228         * @return
229         *     OC_STACK_OK    - no errors
230         *     OC_STACK_ERROR - stack process error
231         */
232         OCStackResult setDefaultDeviceEntityHandler(EntityHandler entityHandler);
233
234         /**
235         * This API unregisters a resource with the server
236         * NOTE: This API applies to server side only.
237         *
238         * @param resourceHandle - This is the resource handle which we which to unregister from the
239         *                           server
240         *
241         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
242         * NOTE: OCStackResult is defined in ocstack.h.
243         */
244         OCStackResult unregisterResource(const OCResourceHandle& resourceHandle) const;
245
246         /**
247         * Add a resource to a collection resource.
248         *
249         * @param collectionHandle - handle to the collection resource
250         * @param addedResourceHandle - handle to resource to be added to the collection resource
251         *
252         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.<br>
253         * NOTE: OCStackResult is defined in ocstack.h. <br>
254         * NOTE: bindResource must be used only after the both collection resource and
255         * resource to add under a collections are created and respective handles obtained<br>
256         * <b>Example:</b> <br>
257         * Step 1: registerResource(homeResourceHandle, "a/home", "home", Link_Interface,
258         *               entityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
259         * Step 2: registerResource(kitchenResourceHandle, "a/kitchen", "kitchen", Link_Interface,
260         *               entityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
261         * Step 3: bindResource(homeResourceHandle, kitchenResourceHandle);<br>
262         * At the end of Step 3, resource "a/home" will contain a reference to "a/kitchen".<br>
263         */
264         OCStackResult bindResource(const OCResourceHandle collectionHandle,
265                     const OCResourceHandle resourceHandle);
266
267         /**
268         * Add multiple resources to a collection resource.
269         *
270         * @param collectionHandle - handle to the collection resource
271         * @param addedResourceHandleList reference to list of resource handles to be added to
272         *       the collection resource
273         *
274         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. <br>
275         * NOTE: OCStackResult is defined in ocstack.h. <br>
276         * NOTE: bindResources must be used only after the both collection resource and
277         * list of resources to add under a collection are created and respective handles
278         * obtained<br>
279         * <b> Example: </b> <br>
280         * Step 1: registerResource(homeResourceHandle, "a/home", "home", Link_Interface,
281         *           homeEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
282         * Step 2: registerResource(kitchenResourceHandle, "a/kitchen", "kitchen", Link_Interface,
283         *           kitchenEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
284         * Step 3: registerResource(roomResourceHandle, "a/room", "room", Link_Interface,
285         *           roomEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
286         * Step 4: std::vector<OCResourceHandle> rList; rList.push_back(kitchenResourceHandle);
287         *           rList.push_back(roomResourceHandle);<br>
288         * Step 5: bindResource(homeResourceHandle, rList);<br>
289         * At the end of Step 5, resource "a/home" will contain a references to "a/kitchen"
290         *           and "a/room" <br>
291         */
292         OCStackResult bindResources(const OCResourceHandle collectionHandle,
293                     const std::vector<OCResourceHandle>& addedResourceHandleList);
294
295         /**
296         * Unbind a resource from a collection resource.
297         *
298         * @param collectionHandle - handle to the collection resource
299         * @param resourceHandle resource handle to be unbound from the collection resource
300         *
301         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. <br>
302         * NOTE: OCStackResult is defined in ocstack.h.<br>
303         * NOTE: unbindResource must be used only after the both collection resource and
304         * resource to unbind from a collection are created and respective handles obtained<br>
305         * <b> Example </b> <br>
306         * Step 1: registerResource(homeResourceHandle, "a/home", "home", Link_Interface,
307         *       entityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
308         * Step 2: registerResource(kitchenResourceHandle, "a/kitchen", "kitchen", Link_Interface,
309         *       entityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
310         * Step 3: bindResource(homeResourceHandle, kitchenResourceHandle);<br>
311         * Step 4: unbindResource(homeResourceHandle, kitchenResourceHandle);<br>
312         * At the end of Step 4, resource "a/home" will no longer reference "a/kitchen". <br>
313         */
314         OCStackResult unbindResource(const OCResourceHandle collectionHandle,
315                     const OCResourceHandle resourceHandle);
316
317         /**
318         * Unbind resources from a collection resource.
319         *
320         * @param collectionHandle - handle to the collection resource
321         * @param resourceHandleList List of resource handles to be unbound from the collection
322         *   resource
323         *
324         * @return OCStackResult return value of this API. Returns OC_STACK_OK if success. <br>
325         *
326         * NOTE: OCStackResult is defined in ocstack.h.<br>
327         * NOTE: unbindResources must be used only after the both collection resource and
328         * list of resources resource to unbind from a collection are created and respective handles
329         * obtained. <br>
330         * <b>Example</b> <br>
331         * Step 1: registerResource(homeResourceHandle, "a/home", "home", Link_Interface,
332         *       homeEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
333         * Step 2: registerResource(kitchenResourceHandle, "a/kitchen", "kitchen", Link_Interface,
334         *       kitchenEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
335         * Step 3: registerResource(roomResourceHandle, "a/room", "room", Link_Interface,
336         *       roomEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);<br>
337         * Step 4: std::vector<OCResourceHandle> rList; rList.push_back(kitchenResourceHandle);
338         *       rList.push_back(roomResourceHandle);<br>
339         * Step 5: bindResource(homeResourceHandle, rList);<br>
340         * Step 6: unbindResources(homeResourceHandle, rList);<br>
341         * At the end of Step 6, resource "a/home" will no longer reference to "a/kitchen"
342         *       and "a/room"<br>
343         */
344         OCStackResult unbindResources(const OCResourceHandle collectionHandle,
345                         const std::vector<OCResourceHandle>& resourceHandleList);
346
347         /**
348         * Binds a type to a particular resource
349         * @param resourceHandle - handle to the resource
350         * @param resourceTypeName - new typename to bind to the resource
351
352         * @return OCStackResult - return value of the API. Returns OCSTACK_OK if success <br>
353         */
354         OCStackResult bindTypeToResource(const OCResourceHandle& resourceHandle,
355                         const std::string& resourceTypeName) const;
356
357         /**
358         * Binds an interface to a particular resource
359         * @param resourceHandle - handle to the resource
360         * @param resourceTypeName - new interface  to bind to the resource
361
362         * @return OCStackResult - return value of the API. Returns OCSTACK_OK if success <br>
363         */
364         OCStackResult bindInterfaceToResource(const OCResourceHandle& resourceHandle,
365                         const std::string& resourceInterfaceName) const;
366
367         /**
368         * Start Presence announcements.
369         *
370         * @param ttl - time to live
371         * @return OCStackResult - Returns OCSTACK_OK if success <br>
372         *
373         * Server can call this function when it comes online for the
374         * first time, or when it comes back online from offline mode,
375         * or when it re enters network.
376         *
377         */
378         OCStackResult startPresence(const unsigned int ttl);
379
380         /**
381         * Stop Presence announcements.
382         *
383         * @return OCStackResult - Returns OCSTACK_OK if success <br>
384         *
385         * Server can call this function when it is terminating,
386         * going offline, or when going away from network.
387         *
388         */
389         OCStackResult stopPresence();
390
391         /**
392         * subscribes to a server's presence change events.  By making this subscription,
393         * every time a server adds/removes/alters a resource, starts or is intentionally
394         * stopped (potentially more to be added later).
395         *
396         * @param presenceHandle - a handle object that can be used to identify this subscription
397         *               request.  It can be used to unsubscribe from these events in the future.
398         *               It will be set upon successful return of this method.
399         * @param host - The IP address/addressable name of the server to subscribe to.
400         * @param resourceType - a resource type specified as a filter for subscription callbacks.
401         * @param presenceHandler - callback function that will receive notifications/subscription
402         *                           events
403         *
404         * @return OCStackResult - return value of the API.  Returns OCSTACK_OK if success <br>
405         */
406         OCStackResult subscribePresence(OCPresenceHandle& presenceHandle, const std::string& host,
407                         SubscribeCallback presenceHandler);
408         OCStackResult subscribePresence(OCPresenceHandle& presenceHandle, const std::string& host,
409                         const std::string& resourceType, SubscribeCallback presenceHandler);
410
411         /**
412         * unsubscribes from a previously subscribed server's presence events. Note that
413         * you may for a short time still receive events from the server since it may take time
414         * for the unsubscribe to take effect.
415         *
416         * @param presenceHandle - the handle object provided by the subscribePresence call that
417         *               identifies this subscription.
418         *
419         * @return OCStackResult - return value of the API.  Returns OCSTACK_OK if success <br>
420         */
421         OCStackResult unsubscribePresence(OCPresenceHandle presenceHandle);
422
423         /**
424         * Creates a resource proxy object so that get/put/observe functionality
425         * can be used without discovering the object in advance.  Note that the
426         * consumer of this method needs to provide all of the details required to
427         * correctly contact and observe the object. If the consumer lacks any of
428         * this information, they should discover the resource object normally.
429         * Additionally, you can only create this object if OCPlatform was initialized
430         * to be a Client or Client/Server.  Otherwise, this will return an empty
431         * shared ptr.
432         *
433         * @param host - a string containing a resolvable host address of the server
434         *           holding the resource. Currently this should be in the format
435         *           coap://address:port, though in the future, we expect this to
436         *           change to //address:port
437         *
438         * @param uri - the rest of the resource's URI that will permit messages to be
439         *           properly routed.  Example: /a/light
440         *
441         * @param isObservable - a boolean containing whether the resource supports observation
442         *
443         * @param resourceTypes - a collection of resource types implemented by the resource
444         *
445         * @param interfaces - a collection of interfaces that the resource supports/implements
446         * @return OCResource::Ptr - a shared pointer to the new resource object
447         */
448         OCResource::Ptr constructResourceObject(const std::string& host, const std::string& uri,
449                         bool isObservable, const std::vector<std::string>& resourceTypes,
450                         const std::vector<std::string>& interfaces);
451
452         /**
453         * Allows application entity handler to send response to an incoming request.
454         *
455         * @param pResponse - OCResourceResponse pointer that will permit to set values related
456         * to resource response. <br>
457         * @return OCStackResult - return value of the API. Returns OCSTACK_OK if success <br>
458         */
459         OCStackResult sendResponse(const std::shared_ptr<OCResourceResponse> pResponse);
460
461     private:
462         PlatformConfig m_cfg;
463
464     private:
465         std::unique_ptr<WrapperFactory> m_WrapperInstance;
466         IServerWrapper::Ptr m_server;
467         IClientWrapper::Ptr m_client;
468         std::shared_ptr<std::recursive_mutex> m_csdkLock;
469
470     private:
471         /**
472         * Constructor for OCPlatform. Constructs a new OCPlatform from a given PlatformConfig with
473         * appropriate fields
474         * @param config PlatformConfig struct which has details such as modeType
475         * (server/client/both), in-proc/out-of-proc etc.
476         */
477         OCPlatform_impl(const PlatformConfig& config);
478
479         /**
480         * Private function to initalize the platfrom
481         */
482         void init(const PlatformConfig& config);
483
484         /**
485         * Private constructor/operators to prevent copying
486         * of this object
487         */
488         OCPlatform_impl(const OCPlatform_impl& other)= delete;
489         OCPlatform_impl& operator=(const OCPlatform_impl&) = delete;
490         OCPlatform_impl& operator=(const OCPlatform_impl&&) = delete;
491     };
492 }
493
494 #endif //__OCPLATFORM_IMPL_H
495
496