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