Remove resource directory dependency in lib OC
[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 findResourceList(const std::string& host, const std::string& resourceURI,
96                     OCConnectivityType connectivityType, FindResListCallback resourceHandler,
97                     QualityOfService QoS);
98
99         OCStackResult getDeviceInfo(const std::string& host, const std::string& deviceURI,
100                     OCConnectivityType connectivityType, FindDeviceCallback deviceInfoHandler);
101
102         OCStackResult getDeviceInfo(const std::string& host, const std::string& deviceURI,
103                     OCConnectivityType connectivityType, FindDeviceCallback deviceInfoHandler,
104                     QualityOfService QoS);
105
106         OCStackResult getPlatformInfo(const std::string& host, const std::string& platformURI,
107                     OCConnectivityType connectivityType, FindPlatformCallback platformInfoHandler);
108
109         OCStackResult getPlatformInfo(const std::string& host, const std::string& platformURI,
110                     OCConnectivityType connectivityType, FindPlatformCallback platformInfoHandler,
111                     QualityOfService QoS);
112
113         /**
114          * API for Device Discovery
115          *
116          * @param host Host IP Address. If null or empty, Multicast is performed.
117          * @param deviceURI Uri containing address to the virtual device in C Stack
118          *                       ("/oic/d")
119          * @param deviceInfoHandler device discovery callback
120          * @param QualityOfService the quality of communication
121          * @return Returns ::OC_STACK_OK if success.
122          * @note OCStackResult is defined in ocstack.h.
123          */
124         OCStackResult getDeviceInfo(const std::string& host, const std::string& deviceURI,
125                     FindDeviceCallback deviceInfoHandler);
126         OCStackResult getDeviceInfo(const std::string& host, const std::string& deviceURI,
127                     FindDeviceCallback deviceInfoHandler, QualityOfService QoS);
128
129         /**
130          * API for Platform Discovery
131          *
132          * @param host Host IP Address. If null or empty, Multicast is performed.
133          * @param platformURI Uri containing address to the virtual platform in C Stack
134          *                       ("/oic/p")
135          * @param platformInfoHandler platform discovery callback
136          * @param QualityOfService the quality of communication
137          * @return Returns ::OC_STACK_OK if success.
138          * @note OCStackResult is defined in ocstack.h.
139          */
140         OCStackResult getPlatformInfo(const std::string& host, const std::string& platformURI,
141                     FindPlatformCallback platformInfoHandler);
142         OCStackResult getPlatformInfo(const std::string& host, const std::string& platformURI,
143                     FindPlatformCallback platformInfoHandler, QualityOfService QoS);
144
145         /**
146         * This API registers a resource with the server
147         * @note This API applies to server side only.
148         *
149         * @param resourceHandle Upon successful registration, resourceHandle will be filled
150         * @param resourceURI The URI of the resource. Example: "a/light". See NOTE below
151         * @param resourceTypeName The resource type. Example: "light"
152         * @param resourceInterface The resource interface (whether it is collection etc).
153         * @param entityHandler entity handler callback.
154         * @param resourceProperty indicates the property of the resource. Defined in ocstack.h.
155         * setting resourceProperty as OC_DISCOVERABLE will allow Discovery of this resource
156         * setting resourceProperty as OC_OBSERVABLE will allow observation
157         * settings resourceProperty as OC_DISCOVERABLE | OC_OBSERVABLE will allow both discovery
158         * and observation
159         *
160         * @return Returns ::OC_STACK_OK if success.
161         * @note "a/light" is a relative URI.
162         * Above relative URI will be prepended (by core) with a host IP + namespace "oc"
163         * Therefore, fully qualified URI format would be //HostIP-Address/namespace/relativeURI"
164         * Example, a relative URI: 'a/light' will result in a fully qualified URI:
165         *   //192.168.1.1/oic/a/light"
166         * First parameter can take a relative URI and core will take care of preparing the fully
167         * qualified URI OR
168         * first parameter can take fully qualified URI and core will take that as is for further
169         * operations
170         * @note OCStackResult is defined in ocstack.h.
171         */
172         OCStackResult registerResource(OCResourceHandle& resourceHandle,
173                         std::string& resourceURI,
174                         const std::string& resourceTypeName,
175                         const std::string& resourceInterface,
176                         EntityHandler entityHandler,
177                         uint8_t resourceProperty);
178
179         OCStackResult registerResource(OCResourceHandle& resourceHandle,
180                         const std::shared_ptr<OCResource> resource);
181
182         /**
183          * This API registers all the device specific information
184          *
185          * @param deviceInfo Structure containing all the device related information
186          *
187          * @return Returns ::OC_STACK_OK if success
188          * @note OCDeviceInfo is defined in OCStack.h
189          */
190         OCStackResult registerDeviceInfo(const OCDeviceInfo deviceInfo);
191
192         /**
193          * This API registers all the platform specific information
194          *
195          * @param platformInfo Structure containing all the platform related information
196          *
197          * @return Returns ::OC_STACK_OK if success
198          * @note OCPlatformInfo is defined in OCStack.h
199          */
200         OCStackResult registerPlatformInfo(const OCPlatformInfo platformInfo);
201
202         OCStackResult setDefaultDeviceEntityHandler(EntityHandler entityHandler);
203
204         OCStackResult unregisterResource(const OCResourceHandle& resourceHandle) const;
205
206         OCStackResult bindResource(const OCResourceHandle collectionHandle,
207                     const OCResourceHandle resourceHandle);
208
209         OCStackResult bindResources(const OCResourceHandle collectionHandle,
210                     const std::vector<OCResourceHandle>& addedResourceHandleList);
211
212         OCStackResult unbindResource(const OCResourceHandle collectionHandle,
213                     const OCResourceHandle resourceHandle);
214
215         OCStackResult unbindResources(const OCResourceHandle collectionHandle,
216                         const std::vector<OCResourceHandle>& resourceHandleList);
217
218         OCStackResult bindTypeToResource(const OCResourceHandle& resourceHandle,
219                         const std::string& resourceTypeName) const;
220
221         OCStackResult bindInterfaceToResource(const OCResourceHandle& resourceHandle,
222                         const std::string& resourceInterfaceName) const;
223
224         OCStackResult startPresence(const unsigned int ttl);
225
226         OCStackResult stopPresence();
227
228         OCStackResult subscribePresence(OCPresenceHandle& presenceHandle, const std::string& host,
229                         OCConnectivityType connectivityType, SubscribeCallback presenceHandler);
230
231         OCStackResult subscribePresence(OCPresenceHandle& presenceHandle, const std::string& host,
232                         const std::string& resourceType, OCConnectivityType connectivityType,
233                         SubscribeCallback presenceHandler);
234         OCStackResult unsubscribePresence(OCPresenceHandle presenceHandle);
235
236 #ifdef WITH_CLOUD
237         OCStackResult subscribeDevicePresence(OCPresenceHandle& presenceHandle,
238                                               const std::string& host,
239                                               const std::vector<std::string>& di,
240                                               OCConnectivityType connectivityType,
241                                               ObserveCallback callback);
242 #endif
243
244         OCResource::Ptr constructResourceObject(const std::string& host, const std::string& uri,
245                         OCConnectivityType connectivityType, bool isObservable,
246                         const std::vector<std::string>& resourceTypes,
247                         const std::vector<std::string>& interfaces);
248         OCStackResult sendResponse(const std::shared_ptr<OCResourceResponse> pResponse);
249         std::weak_ptr<std::recursive_mutex> csdkLock();
250
251         OCStackResult findDirectPairingDevices(unsigned short waittime,
252                                          GetDirectPairedCallback callback);
253
254         OCStackResult getDirectPairedDevices(GetDirectPairedCallback callback);
255
256         OCStackResult doDirectPairing(std::shared_ptr<OCDirectPairing> peer, OCPrm_t pmSel,
257                                          const std::string& pinNumber,
258                                          DirectPairingCallback resultCallback);
259 #ifdef WITH_CLOUD
260         OCAccountManager::Ptr constructAccountManagerObject(const std::string& host,
261                                                             OCConnectivityType connectivityType);
262 #endif // WITH_CLOUD
263
264         OCStackResult getDeviceId(OCUUIdentity *myUuid);
265
266         OCStackResult setDeviceId(const OCUUIdentity *myUuid);
267
268     private:
269         PlatformConfig m_cfg;
270
271     private:
272         std::unique_ptr<WrapperFactory> m_WrapperInstance;
273         IServerWrapper::Ptr m_server;
274         IClientWrapper::Ptr m_client;
275         std::shared_ptr<std::recursive_mutex> m_csdkLock;
276
277     private:
278         /**
279         * Constructor for OCPlatform_impl. Constructs a new OCPlatform_impl from a given
280         * PlatformConfig with appropriate fields
281         * @param config PlatformConfig struct which has details such as modeType
282         * (server/client/both), in-proc/out-of-proc etc.
283         */
284         OCPlatform_impl(const PlatformConfig& config);
285
286         /**
287         * Private function to initialize the platform
288         */
289         void init(const PlatformConfig& config);
290
291         /**
292         * Private constructor/operators to prevent copying
293         * of this object
294         */
295         OCPlatform_impl(const OCPlatform_impl& other)= delete;
296         OCPlatform_impl& operator=(const OCPlatform_impl&) = delete;
297         OCPlatform_impl& operator=(const OCPlatform_impl&&) = delete;
298     };
299 }
300
301 #endif //__OCPLATFORM_IMPL_H