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