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