Merge remote-tracking branch 'origin/extended-easysetup'
[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 OC_PLATFORM_IMPL_H_
30 #define OC_PLATFORM_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 #include "OCDirectPairing.h"
41
42 #ifdef WITH_CLOUD
43 #include "OCAccountManager.h"
44 #endif
45
46 #include "oc_logger.hpp"
47
48 namespace OC
49 {
50     class OCPlatform_impl
51     {
52     private:
53         static PlatformConfig& globalConfig();
54     public:
55         static void Configure(const PlatformConfig& config);
56
57         static OCPlatform_impl& Instance();
58
59     public:
60         // typedef for handle to cancel presence info with
61         typedef OCDoHandle OCPresenceHandle;
62
63         virtual ~OCPlatform_impl(void);
64
65         OCStackResult notifyAllObservers(OCResourceHandle resourceHandle);
66
67         OCStackResult notifyAllObservers(OCResourceHandle resourceHandle, QualityOfService QoS);
68
69         OCStackResult notifyListOfObservers(
70                     OCResourceHandle resourceHandle,
71                     ObservationIds& observationIds,
72                     const std::shared_ptr<OCResourceResponse> responsePtr);
73
74         OCStackResult notifyListOfObservers(
75                     OCResourceHandle resourceHandle,
76                     ObservationIds& observationIds,
77                     const std::shared_ptr<OCResourceResponse> responsePtr,
78                     QualityOfService QoS);
79
80         OCStackResult findResource(const std::string& host, const std::string& resourceURI,
81                     OCConnectivityType connectivityType, FindCallback resourceHandler);
82
83         OCStackResult findResource(const std::string& host, const std::string& resourceURI,
84                     OCConnectivityType connectivityType, FindCallback resourceHandler,
85                     QualityOfService QoS);
86
87         OCStackResult findResource(const std::string& host, const std::string& resourceURI,
88                     OCConnectivityType connectivityType, FindCallback resourceHandler,
89                     FindErrorCallback errorHandler);
90
91         OCStackResult findResource(const std::string& host, const std::string& resourceURI,
92                     OCConnectivityType connectivityType, FindCallback resourceHandler,
93                     FindErrorCallback errorHandler, QualityOfService QoS);
94
95         OCStackResult getDeviceInfo(const std::string& host, const std::string& deviceURI,
96                     OCConnectivityType connectivityType, FindDeviceCallback deviceInfoHandler);
97
98         OCStackResult getDeviceInfo(const std::string& host, const std::string& deviceURI,
99                     OCConnectivityType connectivityType, FindDeviceCallback deviceInfoHandler,
100                     QualityOfService QoS);
101
102         OCStackResult getPlatformInfo(const std::string& host, const std::string& platformURI,
103                     OCConnectivityType connectivityType, FindPlatformCallback platformInfoHandler);
104
105         OCStackResult getPlatformInfo(const std::string& host, const std::string& platformURI,
106                     OCConnectivityType connectivityType, FindPlatformCallback platformInfoHandler,
107                     QualityOfService QoS);
108
109         /**
110          * API for Device Discovery
111          *
112          * @param host Host IP Address. If null or empty, Multicast is performed.
113          * @param deviceURI Uri containing address to the virtual device in C Stack
114          *                       ("/oic/d")
115          * @param deviceInfoHandler device discovery callback
116          * @param QualityOfService the quality of communication
117          * @return Returns ::OC_STACK_OK if success.
118          * @note OCStackResult is defined in ocstack.h.
119          */
120         OCStackResult getDeviceInfo(const std::string& host, const std::string& deviceURI,
121                     FindDeviceCallback deviceInfoHandler);
122         OCStackResult getDeviceInfo(const std::string& host, const std::string& deviceURI,
123                     FindDeviceCallback deviceInfoHandler, QualityOfService QoS);
124
125         /**
126          * API for Platform Discovery
127          *
128          * @param host Host IP Address. If null or empty, Multicast is performed.
129          * @param platformURI Uri containing address to the virtual platform in C Stack
130          *                       ("/oic/p")
131          * @param platformInfoHandler platform discovery callback
132          * @param QualityOfService the quality of communication
133          * @return Returns ::OC_STACK_OK if success.
134          * @note OCStackResult is defined in ocstack.h.
135          */
136         OCStackResult getPlatformInfo(const std::string& host, const std::string& platformURI,
137                     FindPlatformCallback platformInfoHandler);
138         OCStackResult getPlatformInfo(const std::string& host, const std::string& platformURI,
139                     FindPlatformCallback platformInfoHandler, QualityOfService QoS);
140
141         /**
142         * This API registers a resource with the server
143         * @note This API applies to server side only.
144         *
145         * @param resourceHandle Upon successful registration, resourceHandle will be filled
146         * @param resourceURI The URI of the resource. Example: "a/light". See NOTE below
147         * @param resourceTypeName The resource type. Example: "light"
148         * @param resourceInterface The resource interface (whether it is collection etc).
149         * @param entityHandler entity handler callback.
150         * @param resourceProperty indicates the property of the resource. Defined in ocstack.h.
151         * setting resourceProperty as OC_DISCOVERABLE will allow Discovery of this resource
152         * setting resourceProperty as OC_OBSERVABLE will allow observation
153         * settings resourceProperty as OC_DISCOVERABLE | OC_OBSERVABLE will allow both discovery
154         * and observation
155         *
156         * @return Returns ::OC_STACK_OK if success.
157         * @note "a/light" is a relative URI.
158         * Above relative URI will be prepended (by core) with a host IP + namespace "oc"
159         * Therefore, fully qualified URI format would be //HostIP-Address/namespace/relativeURI"
160         * Example, a relative URI: 'a/light' will result in a fully qualified URI:
161         *   //192.168.1.1/oic/a/light"
162         * First parameter can take a relative URI and core will take care of preparing the fully
163         * qualified URI OR
164         * first parameter can take fully qualified URI and core will take that as is for further
165         * operations
166         * @note OCStackResult is defined in ocstack.h.
167         */
168         OCStackResult registerResource(OCResourceHandle& resourceHandle,
169                         std::string& resourceURI,
170                         const std::string& resourceTypeName,
171                         const std::string& resourceInterface,
172                         EntityHandler entityHandler,
173                         uint8_t resourceProperty);
174
175         OCStackResult registerResource(OCResourceHandle& resourceHandle,
176                         const std::shared_ptr<OCResource> resource);
177
178         /**
179          * This API registers all the device specific information
180          *
181          * @param deviceInfo Structure containing all the device related information
182          *
183          * @return Returns ::OC_STACK_OK if success
184          * @note OCDeviceInfo is defined in OCStack.h
185          */
186         OCStackResult registerDeviceInfo(const OCDeviceInfo deviceInfo);
187
188         /**
189          * This API registers all the platform specific information
190          *
191          * @param platformInfo Structure containing all the platform related information
192          *
193          * @return Returns ::OC_STACK_OK if success
194          * @note OCPlatformInfo is defined in OCStack.h
195          */
196         OCStackResult registerPlatformInfo(const OCPlatformInfo platformInfo);
197
198         OCStackResult setDefaultDeviceEntityHandler(EntityHandler entityHandler);
199
200         OCStackResult unregisterResource(const OCResourceHandle& resourceHandle) const;
201
202         OCStackResult bindResource(const OCResourceHandle collectionHandle,
203                     const OCResourceHandle resourceHandle);
204
205         OCStackResult bindResources(const OCResourceHandle collectionHandle,
206                     const std::vector<OCResourceHandle>& addedResourceHandleList);
207
208         OCStackResult unbindResource(const OCResourceHandle collectionHandle,
209                     const OCResourceHandle resourceHandle);
210
211         OCStackResult unbindResources(const OCResourceHandle collectionHandle,
212                         const std::vector<OCResourceHandle>& resourceHandleList);
213
214         OCStackResult bindTypeToResource(const OCResourceHandle& resourceHandle,
215                         const std::string& resourceTypeName) const;
216
217         OCStackResult bindInterfaceToResource(const OCResourceHandle& resourceHandle,
218                         const std::string& resourceInterfaceName) const;
219
220         OCStackResult startPresence(const unsigned int ttl);
221
222         OCStackResult stopPresence();
223
224         OCStackResult subscribePresence(OCPresenceHandle& presenceHandle, const std::string& host,
225                         OCConnectivityType connectivityType, SubscribeCallback presenceHandler);
226
227         OCStackResult subscribePresence(OCPresenceHandle& presenceHandle, const std::string& host,
228                         const std::string& resourceType, OCConnectivityType connectivityType,
229                         SubscribeCallback presenceHandler);
230         OCStackResult unsubscribePresence(OCPresenceHandle presenceHandle);
231
232 #ifdef WITH_CLOUD
233         OCStackResult subscribeDevicePresence(OCPresenceHandle& presenceHandle,
234                                               const std::string& host,
235                                               const std::vector<std::string>& di,
236                                               OCConnectivityType connectivityType,
237                                               ObserveCallback callback);
238 #endif
239
240         OCResource::Ptr constructResourceObject(const std::string& host, const std::string& uri,
241                         OCConnectivityType connectivityType, bool isObservable,
242                         const std::vector<std::string>& resourceTypes,
243                         const std::vector<std::string>& interfaces);
244         OCStackResult sendResponse(const std::shared_ptr<OCResourceResponse> pResponse);
245 #ifdef RD_CLIENT
246         OCStackResult publishResourceToRD(const std::string& host,
247                                           OCConnectivityType connectivityType,
248                                           ResourceHandles& resourceHandles,
249                                           PublishResourceCallback callback);
250
251         OCStackResult publishResourceToRD(const std::string& host,
252                                           OCConnectivityType connectivityType,
253                                           ResourceHandles& resourceHandles,
254                                           PublishResourceCallback callback, QualityOfService qos);
255
256         OCStackResult deleteResourceFromRD(const std::string& host,
257                                            OCConnectivityType connectivityType,
258                                            ResourceHandles& resourceHandles,
259                                            DeleteResourceCallback callback);
260
261         OCStackResult deleteResourceFromRD(const std::string& host,
262                                            OCConnectivityType connectivityType,
263                                            ResourceHandles& resourceHandles,
264                                            DeleteResourceCallback callback, QualityOfService qos);
265 #endif
266         std::weak_ptr<std::recursive_mutex> csdkLock();
267
268         OCStackResult findDirectPairingDevices(unsigned short waittime,
269                                          GetDirectPairedCallback callback);
270
271         OCStackResult getDirectPairedDevices(GetDirectPairedCallback callback);
272
273         OCStackResult doDirectPairing(std::shared_ptr<OCDirectPairing> peer, OCPrm_t pmSel,
274                                          const std::string& pinNumber,
275                                          DirectPairingCallback resultCallback);
276 #ifdef WITH_CLOUD
277         OCAccountManager::Ptr constructAccountManagerObject(const std::string& host,
278                                                             OCConnectivityType connectivityType);
279 #endif // WITH_CLOUD
280     private:
281         PlatformConfig m_cfg;
282
283     private:
284         std::unique_ptr<WrapperFactory> m_WrapperInstance;
285         IServerWrapper::Ptr m_server;
286         IClientWrapper::Ptr m_client;
287         std::shared_ptr<std::recursive_mutex> m_csdkLock;
288
289     private:
290         /**
291         * Constructor for OCPlatform_impl. Constructs a new OCPlatform_impl from a given
292         * PlatformConfig with appropriate fields
293         * @param config PlatformConfig struct which has details such as modeType
294         * (server/client/both), in-proc/out-of-proc etc.
295         */
296         OCPlatform_impl(const PlatformConfig& config);
297
298         /**
299         * Private function to initialize the platform
300         */
301         void init(const PlatformConfig& config);
302
303         /**
304         * Private constructor/operators to prevent copying
305         * of this object
306         */
307         OCPlatform_impl(const OCPlatform_impl& other)= delete;
308         OCPlatform_impl& operator=(const OCPlatform_impl&) = delete;
309         OCPlatform_impl& operator=(const OCPlatform_impl&&) = delete;
310     };
311 }
312
313 #endif //__OCPLATFORM_IMPL_H
314
315
316