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"
46 #include "oc_logger.hpp"
51 PlatformConfig& OCPlatform_impl::globalConfig()
53 static PlatformConfig s_config;
57 void OCPlatform_impl::Configure(const PlatformConfig& config)
59 globalConfig() = config;
62 OCPlatform_impl& OCPlatform_impl::Instance()
64 static OCPlatform_impl platform(globalConfig());
68 void OCPlatform_impl::init(const PlatformConfig& config)
72 case ModeType::Server:
73 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
76 case ModeType::Client:
77 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
81 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
82 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
87 OCPlatform_impl::OCPlatform_impl(const PlatformConfig& config)
89 m_WrapperInstance { make_unique<WrapperFactory>() },
90 m_csdkLock { std::make_shared<std::recursive_mutex>() }
95 OCPlatform_impl::~OCPlatform_impl(void)
99 OCStackResult OCPlatform_impl::setDefaultDeviceEntityHandler(EntityHandler entityHandler)
101 return checked_guard(m_server, &IServerWrapper::setDefaultDeviceEntityHandler,
105 OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle,
106 QualityOfService QoS)
108 return result_guard(OCNotifyAllObservers(resourceHandle,
109 static_cast<OCQualityOfService>(QoS)));
112 OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle)
114 return notifyAllObservers(resourceHandle, m_cfg.QoS);
117 OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
118 ObservationIds& observationIds,
119 const std::shared_ptr<OCResourceResponse> pResponse)
121 return notifyListOfObservers(resourceHandle, observationIds, pResponse, m_cfg.QoS);
124 OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
125 ObservationIds& observationIds,
126 const std::shared_ptr<OCResourceResponse> pResponse,
127 QualityOfService QoS)
131 return result_guard(OC_STACK_ERROR);
134 std::string payload(pResponse->getResourceRepresentation().getJSONRepresentation());
137 OCNotifyListOfObservers(resourceHandle,
138 &observationIds[0], observationIds.size(),
140 static_cast<OCQualityOfService>(QoS)));
143 OCResource::Ptr OCPlatform_impl::constructResourceObject(const std::string& host,
144 const std::string& uri,
145 OCConnectivityType connectivityType,
147 const std::vector<std::string>& resourceTypes,
148 const std::vector<std::string>& interfaces)
152 return std::shared_ptr<OCResource>();
155 return std::shared_ptr<OCResource>(new OCResource(m_client,
157 uri, "", connectivityType,
163 OCStackResult OCPlatform_impl::findResource(const std::string& host,
164 const std::string& resourceName,
165 OCConnectivityType connectivityType,
166 FindCallback resourceHandler)
168 return findResource(host, resourceName, connectivityType, resourceHandler, m_cfg.QoS);
171 OCStackResult OCPlatform_impl::findResource(const std::string& host,
172 const std::string& resourceName,
173 OCConnectivityType connectivityType,
174 FindCallback resourceHandler, QualityOfService QoS)
177 return checked_guard(m_client, &IClientWrapper::ListenForResource,
178 host, resourceName, connectivityType, resourceHandler, QoS);
181 OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
182 const std::string& deviceURI,
183 OCConnectivityType connectivityType,
184 FindDeviceCallback deviceInfoHandler)
186 return result_guard(getDeviceInfo(host, deviceURI, connectivityType,
187 deviceInfoHandler, m_cfg.QoS));
190 OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
191 const std::string& deviceURI,
192 OCConnectivityType connectivityType,
193 FindDeviceCallback deviceInfoHandler,
194 QualityOfService QoS)
196 return checked_guard(m_client, &IClientWrapper::ListenForDevice,
197 host, deviceURI, connectivityType, deviceInfoHandler, QoS);
200 OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
201 const std::string& platformURI,
202 OCConnectivityType connectivityType,
203 FindPlatformCallback platformInfoHandler)
205 return result_guard(getPlatformInfo(host, platformURI, connectivityType,
206 platformInfoHandler, m_cfg.QoS));
209 OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
210 const std::string& platformURI,
211 OCConnectivityType connectivityType,
212 FindPlatformCallback platformInfoHandler,
213 QualityOfService QoS)
215 return checked_guard(m_client, &IClientWrapper::ListenForDevice,
216 host, platformURI, connectivityType, platformInfoHandler, QoS);
219 OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
220 std::string& resourceURI,
221 const std::string& resourceTypeName,
222 const std::string& resourceInterface,
223 EntityHandler entityHandler,
224 uint8_t resourceProperty)
226 return checked_guard(m_server, &IServerWrapper::registerResource,
227 std::ref(resourceHandle), resourceURI, resourceTypeName,
228 resourceInterface, entityHandler, resourceProperty);
231 OCStackResult OCPlatform_impl::registerDeviceInfo(const OCDeviceInfo deviceInfo)
233 return checked_guard(m_server, &IServerWrapper::registerDeviceInfo, deviceInfo);
236 OCStackResult OCPlatform_impl::registerPlatformInfo(const OCPlatformInfo platformInfo)
238 return checked_guard(m_server, &IServerWrapper::registerPlatformInfo, platformInfo);
241 OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
242 const std::shared_ptr< OCResource > resource)
244 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
245 std::vector<std::string> resourceTypes = resource->getResourceTypes();
247 return checked_guard(m_server, &IServerWrapper::registerResourceWithHost,
248 std::ref(resourceHandle), resource->host(), resource->uri(),
249 resourceTypes[0]/*"core.remote"*/, DEFAULT_INTERFACE,
250 (EntityHandler) nullptr, resourceProperty);
253 OCStackResult OCPlatform_impl::unregisterResource(const OCResourceHandle& resourceHandle) const
255 return checked_guard(m_server, &IServerWrapper::unregisterResource,
259 OCStackResult OCPlatform_impl::unbindResource(OCResourceHandle collectionHandle,
260 OCResourceHandle resourceHandle)
262 return result_guard(OCUnBindResource(std::ref(collectionHandle), std::ref(resourceHandle)));
265 OCStackResult OCPlatform_impl::unbindResources(const OCResourceHandle collectionHandle,
266 const std::vector<OCResourceHandle>& resourceHandles)
268 for(const auto& h : resourceHandles)
272 if(OC_STACK_OK != (r = result_guard(OCUnBindResource(collectionHandle, h))))
281 OCStackResult OCPlatform_impl::bindResource(const OCResourceHandle collectionHandle,
282 const OCResourceHandle resourceHandle)
284 return result_guard(OCBindResource(collectionHandle, resourceHandle));
287 OCStackResult OCPlatform_impl::bindResources(const OCResourceHandle collectionHandle,
288 const std::vector<OCResourceHandle>& resourceHandles)
290 for(const auto& h : resourceHandles)
294 if(OC_STACK_OK != (r = result_guard(OCBindResource(collectionHandle, h))))
303 OCStackResult OCPlatform_impl::bindTypeToResource(const OCResourceHandle& resourceHandle,
304 const std::string& resourceTypeName) const
306 return checked_guard(m_server, &IServerWrapper::bindTypeToResource,
307 resourceHandle, resourceTypeName);
310 OCStackResult OCPlatform_impl::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
311 const std::string& resourceInterfaceName) const
313 return checked_guard(m_server, &IServerWrapper::bindInterfaceToResource,
314 resourceHandle, resourceInterfaceName);
317 OCStackResult OCPlatform_impl::startPresence(const unsigned int announceDurationSeconds)
319 return checked_guard(m_server, &IServerWrapper::startPresence,
320 announceDurationSeconds);
323 OCStackResult OCPlatform_impl::stopPresence()
325 return checked_guard(m_server, &IServerWrapper::stopPresence);
328 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
329 const std::string& host,
330 OCConnectivityType connectivityType,
331 SubscribeCallback presenceHandler)
333 return subscribePresence(presenceHandle, host, "", connectivityType, presenceHandler);
337 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
338 const std::string& host,
339 const std::string& resourceType,
340 OCConnectivityType connectivityType,
341 SubscribeCallback presenceHandler)
343 return checked_guard(m_client, &IClientWrapper::SubscribePresence,
344 &presenceHandle, host, resourceType, connectivityType,
348 OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle presenceHandle)
350 return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
351 std::ref(presenceHandle));
354 OCStackResult OCPlatform_impl::sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
356 return checked_guard(m_server, &IServerWrapper::sendResponse,