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