Merge "Merge branch 'security-summit' into 'master'"
[platform/upstream/iotivity.git] / resource / src / OCPlatform_impl.cpp
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 name:
23 //     OCPlatform_impl.cpp
24 //
25 // Description: Implementation of the OCPlatform functionality.  It contains
26 // a singleton interface that is used only by the OCPlatform namespace and is the
27 // central entrance to the stack.
28 //
29 //
30 //
31 //*********************************************************************
32
33 #include "OCPlatform_impl.h"
34
35 #include <random>
36 #include <utility>
37 #include <functional>
38
39 #include "ocstack.h"
40
41 #include "OCPlatform.h"
42 #include "OCApi.h"
43 #include "OCException.h"
44 #include "OCUtilities.h"
45 #include "ocpayload.h"
46
47 #include "oc_logger.hpp"
48
49 namespace OC
50 {
51
52     PlatformConfig& OCPlatform_impl::globalConfig()
53     {
54         static PlatformConfig s_config;
55         return s_config;
56     }
57
58     void OCPlatform_impl::Configure(const PlatformConfig& config)
59     {
60         OCRegisterPersistentStorageHandler(config.ps);
61         globalConfig() = config;
62     }
63
64     OCPlatform_impl& OCPlatform_impl::Instance()
65     {
66         static OCPlatform_impl platform(globalConfig());
67         return platform;
68     }
69
70     void OCPlatform_impl::init(const PlatformConfig& config)
71     {
72         switch(config.mode)
73         {
74             case ModeType::Server:
75                 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
76                 break;
77
78             case ModeType::Client:
79                 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
80                 break;
81
82             case ModeType::Both:
83                 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
84                 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
85                 break;
86          }
87     }
88
89     OCPlatform_impl::OCPlatform_impl(const PlatformConfig& config)
90      : m_cfg             { config },
91        m_WrapperInstance { make_unique<WrapperFactory>() },
92        m_csdkLock        { std::make_shared<std::recursive_mutex>() }
93     {
94         init(m_cfg);
95     }
96
97     OCPlatform_impl::~OCPlatform_impl(void)
98     {
99     }
100
101     OCStackResult OCPlatform_impl::setDefaultDeviceEntityHandler(EntityHandler entityHandler)
102     {
103         return checked_guard(m_server, &IServerWrapper::setDefaultDeviceEntityHandler,
104                              entityHandler);
105     }
106
107     OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle,
108                                                 QualityOfService QoS)
109     {
110         return result_guard(OCNotifyAllObservers(resourceHandle,
111                     static_cast<OCQualityOfService>(QoS)));
112     }
113
114     OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle)
115     {
116         return notifyAllObservers(resourceHandle, m_cfg.QoS);
117     }
118
119     OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
120                                        ObservationIds& observationIds,
121                                        const std::shared_ptr<OCResourceResponse> pResponse)
122     {
123         return notifyListOfObservers(resourceHandle, observationIds, pResponse, m_cfg.QoS);
124     }
125
126     OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
127                                        ObservationIds& observationIds,
128                                        const std::shared_ptr<OCResourceResponse> pResponse,
129                                        QualityOfService QoS)
130     {
131         if(!pResponse)
132         {
133          return result_guard(OC_STACK_ERROR);
134         }
135
136         OCRepPayload* pl = pResponse->getResourceRepresentation().getPayload();
137         OCStackResult result =
138                    OCNotifyListOfObservers(resourceHandle,
139                             &observationIds[0], observationIds.size(),
140                             pl,
141                             static_cast<OCQualityOfService>(QoS));
142         OCRepPayloadDestroy(pl);
143         return result_guard(result);
144     }
145
146     OCResource::Ptr OCPlatform_impl::constructResourceObject(const std::string& host,
147                                                 const std::string& uri,
148                                                 OCConnectivityType connectivityType,
149                                                 bool isObservable,
150                                                 const std::vector<std::string>& resourceTypes,
151                                                 const std::vector<std::string>& interfaces)
152     {
153         if(!m_client)
154         {
155             return std::shared_ptr<OCResource>();
156         }
157
158         return std::shared_ptr<OCResource>(new OCResource(m_client,
159                                             host,
160                                             uri, "", connectivityType,
161                                             isObservable,
162                                             resourceTypes,
163                                             interfaces));
164     }
165
166     OCStackResult OCPlatform_impl::findResource(const std::string& host,
167                                             const std::string& resourceName,
168                                             OCConnectivityType connectivityType,
169                                             FindCallback resourceHandler)
170     {
171         return findResource(host, resourceName, connectivityType, resourceHandler, m_cfg.QoS);
172     }
173
174     OCStackResult OCPlatform_impl::findResource(const std::string& host,
175                                             const std::string& resourceName,
176                                             OCConnectivityType connectivityType,
177                                             FindCallback resourceHandler,
178                                             QualityOfService QoS)
179     {
180         return checked_guard(m_client, &IClientWrapper::ListenForResource,
181                              host, resourceName, connectivityType, resourceHandler, QoS);
182     }
183
184     OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
185                                             const std::string& deviceURI,
186                                             OCConnectivityType connectivityType,
187                                             FindDeviceCallback deviceInfoHandler)
188     {
189         return result_guard(getDeviceInfo(host, deviceURI, connectivityType,
190                deviceInfoHandler, m_cfg.QoS));
191     }
192
193     OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
194                                             const std::string& deviceURI,
195                                             OCConnectivityType connectivityType,
196                                             FindDeviceCallback deviceInfoHandler,
197                                             QualityOfService QoS)
198     {
199         return checked_guard(m_client, &IClientWrapper::ListenForDevice,
200                              host, deviceURI, connectivityType, deviceInfoHandler, QoS);
201     }
202
203     OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
204                                             const std::string& platformURI,
205                                             OCConnectivityType connectivityType,
206                                             FindPlatformCallback platformInfoHandler)
207     {
208         return result_guard(getPlatformInfo(host, platformURI, connectivityType,
209                platformInfoHandler, m_cfg.QoS));
210     }
211
212     OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
213                                             const std::string& platformURI,
214                                             OCConnectivityType connectivityType,
215                                             FindPlatformCallback platformInfoHandler,
216                                             QualityOfService QoS)
217     {
218         return checked_guard(m_client, &IClientWrapper::ListenForDevice,
219                              host, platformURI, connectivityType, platformInfoHandler, QoS);
220     }
221
222     OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
223                                             std::string& resourceURI,
224                                             const std::string& resourceTypeName,
225                                             const std::string& resourceInterface,
226                                             EntityHandler entityHandler,
227                                             uint8_t resourceProperty)
228     {
229         return checked_guard(m_server, &IServerWrapper::registerResource,
230                              std::ref(resourceHandle), resourceURI, resourceTypeName,
231                              resourceInterface, entityHandler, resourceProperty);
232     }
233
234     OCStackResult OCPlatform_impl::registerDeviceInfo(const OCDeviceInfo deviceInfo)
235     {
236         return checked_guard(m_server, &IServerWrapper::registerDeviceInfo, deviceInfo);
237     }
238
239     OCStackResult OCPlatform_impl::registerPlatformInfo(const OCPlatformInfo platformInfo)
240     {
241         return checked_guard(m_server, &IServerWrapper::registerPlatformInfo, platformInfo);
242     }
243
244     OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
245                                             const std::shared_ptr< OCResource > resource)
246     {
247         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
248         std::vector<std::string> resourceTypes = resource->getResourceTypes();
249
250         return checked_guard(m_server, &IServerWrapper::registerResource,
251                 std::ref(resourceHandle), resource->host() + resource->uri(),
252                 resourceTypes[0]/*"core.remote"*/, DEFAULT_INTERFACE,
253                 (EntityHandler) nullptr, resourceProperty);
254     }
255
256     OCStackResult OCPlatform_impl::unregisterResource(const OCResourceHandle& resourceHandle) const
257     {
258         return checked_guard(m_server, &IServerWrapper::unregisterResource,
259                              resourceHandle);
260     }
261
262     OCStackResult OCPlatform_impl::unbindResource(OCResourceHandle collectionHandle,
263                                             OCResourceHandle resourceHandle)
264     {
265         return result_guard(OCUnBindResource(std::ref(collectionHandle), std::ref(resourceHandle)));
266     }
267
268     OCStackResult OCPlatform_impl::unbindResources(const OCResourceHandle collectionHandle,
269                                             const std::vector<OCResourceHandle>& resourceHandles)
270     {
271         for(const auto& h : resourceHandles)
272         {
273            OCStackResult r;
274
275            if(OC_STACK_OK != (r = result_guard(OCUnBindResource(collectionHandle, h))))
276            {
277                return r;
278            }
279         }
280
281         return OC_STACK_OK;
282     }
283
284     OCStackResult OCPlatform_impl::bindResource(const OCResourceHandle collectionHandle,
285                                             const OCResourceHandle resourceHandle)
286     {
287         return result_guard(OCBindResource(collectionHandle, resourceHandle));
288     }
289
290     OCStackResult OCPlatform_impl::bindResources(const OCResourceHandle collectionHandle,
291                                             const std::vector<OCResourceHandle>& resourceHandles)
292     {
293         for(const auto& h : resourceHandles)
294         {
295            OCStackResult r;
296
297            if(OC_STACK_OK != (r = result_guard(OCBindResource(collectionHandle, h))))
298            {
299                return r;
300            }
301         }
302
303         return OC_STACK_OK;
304     }
305
306     OCStackResult OCPlatform_impl::bindTypeToResource(const OCResourceHandle& resourceHandle,
307                                              const std::string& resourceTypeName) const
308     {
309         return checked_guard(m_server, &IServerWrapper::bindTypeToResource,
310                              resourceHandle, resourceTypeName);
311     }
312
313     OCStackResult OCPlatform_impl::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
314                                              const std::string& resourceInterfaceName) const
315     {
316         return checked_guard(m_server, &IServerWrapper::bindInterfaceToResource,
317                              resourceHandle, resourceInterfaceName);
318     }
319
320     OCStackResult OCPlatform_impl::startPresence(const unsigned int announceDurationSeconds)
321     {
322         return checked_guard(m_server, &IServerWrapper::startPresence,
323                              announceDurationSeconds);
324     }
325
326     OCStackResult OCPlatform_impl::stopPresence()
327     {
328         return checked_guard(m_server, &IServerWrapper::stopPresence);
329     }
330
331     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
332                                             const std::string& host,
333                                             OCConnectivityType connectivityType,
334                                             SubscribeCallback presenceHandler)
335     {
336         return subscribePresence(presenceHandle, host, "", connectivityType, presenceHandler);
337     }
338
339
340     OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
341                                             const std::string& host,
342                                             const std::string& resourceType,
343                                             OCConnectivityType connectivityType,
344                                             SubscribeCallback presenceHandler)
345     {
346         return checked_guard(m_client, &IClientWrapper::SubscribePresence,
347                              &presenceHandle, host, resourceType, connectivityType,
348                              presenceHandler);
349     }
350
351     OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle presenceHandle)
352     {
353         return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
354                              std::ref(presenceHandle));
355     }
356
357     OCStackResult OCPlatform_impl::sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
358     {
359         return checked_guard(m_server, &IServerWrapper::sendResponse,
360                              pResponse);
361     }
362
363     std::weak_ptr<std::recursive_mutex> OCPlatform_impl::csdkLock()
364     {
365         return m_csdkLock;
366     }
367 } //namespace OC
368