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::registerResource(OCResourceHandle& resourceHandle,
201 std::string& resourceURI,
202 const std::string& resourceTypeName,
203 const std::string& resourceInterface,
204 EntityHandler entityHandler,
205 uint8_t resourceProperty)
207 return checked_guard(m_server, &IServerWrapper::registerResource,
208 std::ref(resourceHandle), resourceURI, resourceTypeName,
209 resourceInterface, entityHandler, resourceProperty);
212 OCStackResult OCPlatform_impl::registerDeviceInfo(const OCDeviceInfo deviceInfo)
214 return checked_guard(m_server, &IServerWrapper::registerDeviceInfo, deviceInfo);
217 OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
218 const std::shared_ptr< OCResource > resource)
220 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
221 std::vector<std::string> resourceTypes = resource->getResourceTypes();
223 return checked_guard(m_server, &IServerWrapper::registerResourceWithHost,
224 std::ref(resourceHandle), resource->host(), resource->uri(),
225 resourceTypes[0]/*"core.remote"*/, DEFAULT_INTERFACE,
226 (EntityHandler) nullptr, resourceProperty);
229 OCStackResult OCPlatform_impl::unregisterResource(const OCResourceHandle& resourceHandle) const
231 return checked_guard(m_server, &IServerWrapper::unregisterResource,
235 OCStackResult OCPlatform_impl::unbindResource(OCResourceHandle collectionHandle,
236 OCResourceHandle resourceHandle)
238 return result_guard(OCUnBindResource(std::ref(collectionHandle), std::ref(resourceHandle)));
241 OCStackResult OCPlatform_impl::unbindResources(const OCResourceHandle collectionHandle,
242 const std::vector<OCResourceHandle>& resourceHandles)
244 for(const auto& h : resourceHandles)
248 if(OC_STACK_OK != (r = result_guard(OCUnBindResource(collectionHandle, h))))
257 OCStackResult OCPlatform_impl::bindResource(const OCResourceHandle collectionHandle,
258 const OCResourceHandle resourceHandle)
260 return result_guard(OCBindResource(collectionHandle, resourceHandle));
263 OCStackResult OCPlatform_impl::bindResources(const OCResourceHandle collectionHandle,
264 const std::vector<OCResourceHandle>& resourceHandles)
266 for(const auto& h : resourceHandles)
270 if(OC_STACK_OK != (r = result_guard(OCBindResource(collectionHandle, h))))
279 OCStackResult OCPlatform_impl::bindTypeToResource(const OCResourceHandle& resourceHandle,
280 const std::string& resourceTypeName) const
282 return checked_guard(m_server, &IServerWrapper::bindTypeToResource,
283 resourceHandle, resourceTypeName);
286 OCStackResult OCPlatform_impl::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
287 const std::string& resourceInterfaceName) const
289 return checked_guard(m_server, &IServerWrapper::bindInterfaceToResource,
290 resourceHandle, resourceInterfaceName);
293 OCStackResult OCPlatform_impl::startPresence(const unsigned int announceDurationSeconds)
295 return checked_guard(m_server, &IServerWrapper::startPresence,
296 announceDurationSeconds);
299 OCStackResult OCPlatform_impl::stopPresence()
301 return checked_guard(m_server, &IServerWrapper::stopPresence);
304 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
305 const std::string& host,
306 OCConnectivityType connectivityType,
307 SubscribeCallback presenceHandler)
309 return subscribePresence(presenceHandle, host, "", connectivityType, presenceHandler);
313 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
314 const std::string& host,
315 const std::string& resourceType,
316 OCConnectivityType connectivityType,
317 SubscribeCallback presenceHandler)
319 return checked_guard(m_client, &IClientWrapper::SubscribePresence,
320 &presenceHandle, host, resourceType, connectivityType,
324 OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle presenceHandle)
326 return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
327 std::ref(presenceHandle));
330 OCStackResult OCPlatform_impl::sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
332 return checked_guard(m_server, &IServerWrapper::sendResponse,