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