1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 //******************************************************************
23 // OCPlatform_impl.cpp
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.
31 //*********************************************************************
33 #include "OCPlatform_impl.h"
41 #include "OCPlatform.h"
43 #include "OCException.h"
44 #include "OCUtilities.h"
45 #include "ocpayload.h"
47 #include "oc_logger.hpp"
52 PlatformConfig& OCPlatform_impl::globalConfig()
54 static PlatformConfig s_config;
58 void OCPlatform_impl::Configure(const PlatformConfig& config)
60 globalConfig() = config;
63 OCPlatform_impl& OCPlatform_impl::Instance()
65 static OCPlatform_impl platform(globalConfig());
69 void OCPlatform_impl::init(const PlatformConfig& config)
73 case ModeType::Server:
74 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
77 case ModeType::Client:
78 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
82 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
83 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
88 OCPlatform_impl::OCPlatform_impl(const PlatformConfig& config)
90 m_WrapperInstance { make_unique<WrapperFactory>() },
91 m_csdkLock { std::make_shared<std::recursive_mutex>() }
96 OCPlatform_impl::~OCPlatform_impl(void)
100 OCStackResult OCPlatform_impl::setDefaultDeviceEntityHandler(EntityHandler entityHandler)
102 return checked_guard(m_server, &IServerWrapper::setDefaultDeviceEntityHandler,
106 OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle,
107 QualityOfService QoS)
109 return result_guard(OCNotifyAllObservers(resourceHandle,
110 static_cast<OCQualityOfService>(QoS)));
113 OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle)
115 return notifyAllObservers(resourceHandle, m_cfg.QoS);
118 OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
119 ObservationIds& observationIds,
120 const std::shared_ptr<OCResourceResponse> pResponse)
122 return notifyListOfObservers(resourceHandle, observationIds, pResponse, m_cfg.QoS);
125 OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
126 ObservationIds& observationIds,
127 const std::shared_ptr<OCResourceResponse> pResponse,
128 QualityOfService QoS)
132 return result_guard(OC_STACK_ERROR);
135 OCRepPayload* pl = pResponse->getResourceRepresentation().getPayload();
136 OCStackResult result =
137 OCNotifyListOfObservers(resourceHandle,
138 &observationIds[0], observationIds.size(),
140 static_cast<OCQualityOfService>(QoS));
141 OCRepPayloadDestroy(pl);
142 return result_guard(result);
145 OCResource::Ptr OCPlatform_impl::constructResourceObject(const std::string& host,
146 const std::string& uri,
147 OCConnectivityType connectivityType,
149 const std::vector<std::string>& resourceTypes,
150 const std::vector<std::string>& interfaces)
154 return std::shared_ptr<OCResource>();
157 return std::shared_ptr<OCResource>(new OCResource(m_client,
159 uri, "", connectivityType,
165 OCStackResult OCPlatform_impl::findResource(const std::string& host,
166 const std::string& resourceName,
167 OCConnectivityType connectivityType,
168 FindCallback resourceHandler)
170 return findResource(host, resourceName, connectivityType, resourceHandler, m_cfg.QoS);
173 OCStackResult OCPlatform_impl::findResource(const std::string& host,
174 const std::string& resourceName,
175 OCConnectivityType connectivityType,
176 FindCallback resourceHandler,
177 QualityOfService QoS)
179 return checked_guard(m_client, &IClientWrapper::ListenForResource,
180 host, resourceName, connectivityType, resourceHandler, QoS);
183 OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
184 const std::string& deviceURI,
185 OCConnectivityType connectivityType,
186 FindDeviceCallback deviceInfoHandler)
188 return result_guard(getDeviceInfo(host, deviceURI, connectivityType,
189 deviceInfoHandler, m_cfg.QoS));
192 OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
193 const std::string& deviceURI,
194 OCConnectivityType connectivityType,
195 FindDeviceCallback deviceInfoHandler,
196 QualityOfService QoS)
198 return checked_guard(m_client, &IClientWrapper::ListenForDevice,
199 host, deviceURI, connectivityType, deviceInfoHandler, QoS);
202 OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
203 const std::string& platformURI,
204 OCConnectivityType connectivityType,
205 FindPlatformCallback platformInfoHandler)
207 return result_guard(getPlatformInfo(host, platformURI, connectivityType,
208 platformInfoHandler, m_cfg.QoS));
211 OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
212 const std::string& platformURI,
213 OCConnectivityType connectivityType,
214 FindPlatformCallback platformInfoHandler,
215 QualityOfService QoS)
217 return checked_guard(m_client, &IClientWrapper::ListenForDevice,
218 host, platformURI, connectivityType, platformInfoHandler, QoS);
221 OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
222 std::string& resourceURI,
223 const std::string& resourceTypeName,
224 const std::string& resourceInterface,
225 EntityHandler entityHandler,
226 uint8_t resourceProperty)
228 return checked_guard(m_server, &IServerWrapper::registerResource,
229 std::ref(resourceHandle), resourceURI, resourceTypeName,
230 resourceInterface, entityHandler, resourceProperty);
233 OCStackResult OCPlatform_impl::registerDeviceInfo(const OCDeviceInfo deviceInfo)
235 return checked_guard(m_server, &IServerWrapper::registerDeviceInfo, deviceInfo);
238 OCStackResult OCPlatform_impl::registerPlatformInfo(const OCPlatformInfo platformInfo)
240 return checked_guard(m_server, &IServerWrapper::registerPlatformInfo, platformInfo);
243 OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
244 const std::shared_ptr< OCResource > resource)
246 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
247 std::vector<std::string> resourceTypes = resource->getResourceTypes();
249 return checked_guard(m_server, &IServerWrapper::registerResource,
250 std::ref(resourceHandle), resource->host() + resource->uri(),
251 resourceTypes[0]/*"core.remote"*/, DEFAULT_INTERFACE,
252 (EntityHandler) nullptr, resourceProperty);
255 OCStackResult OCPlatform_impl::unregisterResource(const OCResourceHandle& resourceHandle) const
257 return checked_guard(m_server, &IServerWrapper::unregisterResource,
261 OCStackResult OCPlatform_impl::unbindResource(OCResourceHandle collectionHandle,
262 OCResourceHandle resourceHandle)
264 return result_guard(OCUnBindResource(std::ref(collectionHandle), std::ref(resourceHandle)));
267 OCStackResult OCPlatform_impl::unbindResources(const OCResourceHandle collectionHandle,
268 const std::vector<OCResourceHandle>& resourceHandles)
270 for(const auto& h : resourceHandles)
274 if(OC_STACK_OK != (r = result_guard(OCUnBindResource(collectionHandle, h))))
283 OCStackResult OCPlatform_impl::bindResource(const OCResourceHandle collectionHandle,
284 const OCResourceHandle resourceHandle)
286 return result_guard(OCBindResource(collectionHandle, resourceHandle));
289 OCStackResult OCPlatform_impl::bindResources(const OCResourceHandle collectionHandle,
290 const std::vector<OCResourceHandle>& resourceHandles)
292 for(const auto& h : resourceHandles)
296 if(OC_STACK_OK != (r = result_guard(OCBindResource(collectionHandle, h))))
305 OCStackResult OCPlatform_impl::bindTypeToResource(const OCResourceHandle& resourceHandle,
306 const std::string& resourceTypeName) const
308 return checked_guard(m_server, &IServerWrapper::bindTypeToResource,
309 resourceHandle, resourceTypeName);
312 OCStackResult OCPlatform_impl::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
313 const std::string& resourceInterfaceName) const
315 return checked_guard(m_server, &IServerWrapper::bindInterfaceToResource,
316 resourceHandle, resourceInterfaceName);
319 OCStackResult OCPlatform_impl::startPresence(const unsigned int announceDurationSeconds)
321 return checked_guard(m_server, &IServerWrapper::startPresence,
322 announceDurationSeconds);
325 OCStackResult OCPlatform_impl::stopPresence()
327 return checked_guard(m_server, &IServerWrapper::stopPresence);
330 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
331 const std::string& host,
332 OCConnectivityType connectivityType,
333 SubscribeCallback presenceHandler)
335 return subscribePresence(presenceHandle, host, "", connectivityType, presenceHandler);
339 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
340 const std::string& host,
341 const std::string& resourceType,
342 OCConnectivityType connectivityType,
343 SubscribeCallback presenceHandler)
345 return checked_guard(m_client, &IClientWrapper::SubscribePresence,
346 &presenceHandle, host, resourceType, connectivityType,
350 OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle presenceHandle)
352 return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
353 std::ref(presenceHandle));
356 OCStackResult OCPlatform_impl::sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
358 return checked_guard(m_server, &IServerWrapper::sendResponse,