Merge branch 'master' into notification-service
[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 #ifdef RD_CLIENT
242         OCStackResult publishResourceToRD(const std::string& host,
243                                           OCConnectivityType connectivityType,
244                                           ResourceHandles& resourceHandles,
245                                           PublishResourceCallback callback);
246
247         OCStackResult publishResourceToRD(const std::string& host,
248                                           OCConnectivityType connectivityType,
249                                           ResourceHandles& resourceHandles,
250                                           PublishResourceCallback callback, QualityOfService qos);
251
252         OCStackResult deleteResourceFromRD(const std::string& host,
253                                            OCConnectivityType connectivityType,
254                                            DeleteResourceCallback callback);
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         OCStackResult signUp(const std::string& host,
278                              const std::string& authProvider,
279                              const std::string& authCode,
280                              OCConnectivityType connectivityType,
281                              PostCallback cloudConnectHandler);
282
283         OCStackResult signIn(const std::string& host,
284                              const std::string& accessToken,
285                              OCConnectivityType connectivityType,
286                              PostCallback cloudConnectHandler);
287
288         OCStackResult signOut(const std::string& host,
289                               const std::string& accessToken,
290                               OCConnectivityType connectivityType,
291                               PostCallback cloudConnectHandler);
292
293         OCStackResult signInOut(const std::string& host,
294                                 const std::string& accessToken,
295                                 bool isSignIn,
296                                 OCConnectivityType connectivityType,
297                                 PostCallback cloudConnectHandler);
298
299         OCStackResult refreshAccessToken(const std::string& host,
300                                          const std::string& refreshToken,
301                                          OCConnectivityType connectivityType,
302                                          PostCallback cloudConnectHandler);
303 #endif // WITH_CLOUD
304     private:
305         PlatformConfig m_cfg;
306
307     private:
308         std::unique_ptr<WrapperFactory> m_WrapperInstance;
309         IServerWrapper::Ptr m_server;
310         IClientWrapper::Ptr m_client;
311         std::shared_ptr<std::recursive_mutex> m_csdkLock;
312
313     private:
314         /**
315         * Constructor for OCPlatform_impl. Constructs a new OCPlatform_impl from a given
316         * PlatformConfig with appropriate fields
317         * @param config PlatformConfig struct which has details such as modeType
318         * (server/client/both), in-proc/out-of-proc etc.
319         */
320         OCPlatform_impl(const PlatformConfig& config);
321
322         /**
323         * Private function to initialize the platform
324         */
325         void init(const PlatformConfig& config);
326
327         /**
328         * Private constructor/operators to prevent copying
329         * of this object
330         */
331         OCPlatform_impl(const OCPlatform_impl& other)= delete;
332         OCPlatform_impl& operator=(const OCPlatform_impl&) = delete;
333         OCPlatform_impl& operator=(const OCPlatform_impl&&) = delete;
334     };
335 }
336
337 #endif //__OCPLATFORM_IMPL_H
338
339
340