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