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 //*********************************************************************
39 #include "OCPlatform.h"
41 #include "OCException.h"
42 #include "OCUtilities.h"
44 #include "oc_logger.hpp"
49 PlatformConfig& OCPlatform_impl::globalConfig()
51 static PlatformConfig s_config;
55 void OCPlatform_impl::Configure(const PlatformConfig& config)
57 globalConfig() = config;
60 OCPlatform_impl& OCPlatform_impl::Instance()
62 static OCPlatform_impl platform(globalConfig());
66 void OCPlatform_impl::init(const PlatformConfig& config)
70 case ModeType::Server:
71 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
74 case ModeType::Client:
75 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
79 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
80 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
85 OCPlatform_impl::OCPlatform_impl(const PlatformConfig& config)
87 m_WrapperInstance { make_unique<WrapperFactory>() },
88 m_csdkLock { make_shared<std::recursive_mutex>() }
93 OCPlatform_impl::~OCPlatform_impl(void)
97 OCStackResult OCPlatform_impl::setDefaultDeviceEntityHandler(EntityHandler entityHandler)
99 return checked_guard(m_server, &IServerWrapper::setDefaultDeviceEntityHandler,
103 OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle,
104 QualityOfService QoS)
106 return result_guard(OCNotifyAllObservers(resourceHandle,
107 static_cast<OCQualityOfService>(QoS)));
110 OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle)
112 return notifyAllObservers(resourceHandle, m_cfg.QoS);
115 OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
116 ObservationIds& observationIds,
117 const std::shared_ptr<OCResourceResponse> pResponse)
119 return notifyListOfObservers(resourceHandle, observationIds, pResponse, m_cfg.QoS);
122 OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
123 ObservationIds& observationIds,
124 const std::shared_ptr<OCResourceResponse> pResponse,
125 QualityOfService QoS)
129 return result_guard(OC_STACK_ERROR);
132 std::string payload(pResponse->getResourceRepresentation().getJSONRepresentation());
135 OCNotifyListOfObservers(resourceHandle,
136 &observationIds[0], observationIds.size(),
137 reinterpret_cast<unsigned char *>(const_cast<char *>(payload.c_str())),
138 static_cast<OCQualityOfService>(QoS)));
142 OCResource::Ptr OCPlatform_impl::constructResourceObject(const std::string& host,
143 const std::string& uri, uint8_t connectivityType,
145 const std::vector<std::string>& resourceTypes,
146 const std::vector<std::string>& interfaces)
150 return std::shared_ptr<OCResource>();
153 return std::shared_ptr<OCResource>(new OCResource(m_client,
155 uri, connectivityType,
161 OCStackResult OCPlatform_impl::findResource(const std::string& host,
162 const std::string& resourceName,
163 uint8_t connectivityType,
164 FindCallback resourceHandler)
166 return findResource(host, resourceName, connectivityType, resourceHandler, m_cfg.QoS);
169 OCStackResult OCPlatform_impl::findResource(const std::string& host,
170 const std::string& resourceName,
171 uint8_t connectivityType,
172 FindCallback resourceHandler, QualityOfService QoS)
175 return checked_guard(m_client, &IClientWrapper::ListenForResource,
176 host, resourceName, connectivityType, resourceHandler, QoS);
179 OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
180 const std::string& deviceURI,
181 uint8_t connectivityType,
182 FindDeviceCallback deviceInfoHandler)
184 return result_guard(getDeviceInfo(host, deviceURI, connectivityType,
185 deviceInfoHandler, m_cfg.QoS));
188 OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
189 const std::string& deviceURI,
190 uint8_t connectivityType,
191 FindDeviceCallback deviceInfoHandler,
192 QualityOfService QoS)
194 return checked_guard(m_client, &IClientWrapper::ListenForDevice,
195 host, deviceURI, connectivityType, deviceInfoHandler, QoS);
199 OCResource::Ptr OCPlatform_impl::constructResourceObject(const std::string& host,
200 const std::string& uri,
202 const std::vector<std::string>& resourceTypes,
203 const std::vector<std::string>& interfaces)
207 return std::shared_ptr<OCResource>();
210 return std::shared_ptr<OCResource>(new OCResource(m_client,
218 OCStackResult OCPlatform_impl::findResource(const std::string& host,
219 const std::string& resourceName,
220 FindCallback resourceHandler)
222 return findResource(host, resourceName, resourceHandler, m_cfg.QoS);
225 OCStackResult OCPlatform_impl::findResource(const std::string& host,
226 const std::string& resourceName,
227 FindCallback resourceHandler, QualityOfService QoS)
230 return checked_guard(m_client, &IClientWrapper::ListenForResource,
231 host, resourceName, resourceHandler, QoS);
234 OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
235 const std::string& deviceURI,
236 FindDeviceCallback deviceInfoHandler)
238 return result_guard(getDeviceInfo(host, deviceURI, deviceInfoHandler, m_cfg.QoS));
241 OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
242 const std::string& deviceURI,
243 FindDeviceCallback deviceInfoHandler,
244 QualityOfService QoS)
246 return checked_guard(m_client, &IClientWrapper::ListenForDevice,
247 host, deviceURI, deviceInfoHandler, QoS);
252 OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
253 std::string& resourceURI,
254 const std::string& resourceTypeName,
255 const std::string& resourceInterface,
256 EntityHandler entityHandler,
257 uint8_t resourceProperty)
259 return checked_guard(m_server, &IServerWrapper::registerResource,
260 ref(resourceHandle), resourceURI, resourceTypeName,
261 resourceInterface, entityHandler, resourceProperty);
264 OCStackResult OCPlatform_impl::registerDeviceInfo(const OCDeviceInfo deviceInfo)
266 return checked_guard(m_server, &IServerWrapper::registerDeviceInfo, deviceInfo);
269 OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
270 const std::shared_ptr< OCResource > resource)
272 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
273 std::vector<std::string> resourceTypes = resource->getResourceTypes();
275 return checked_guard(m_server, &IServerWrapper::registerResourceWithHost,
276 ref(resourceHandle), resource->host(), resource->uri(), resourceTypes[0]/*"core.remote"*/, "oc.mi.def",
277 (EntityHandler) nullptr, resourceProperty);
280 OCStackResult OCPlatform_impl::unregisterResource(const OCResourceHandle& resourceHandle) const
282 return checked_guard(m_server, &IServerWrapper::unregisterResource,
286 OCStackResult OCPlatform_impl::unbindResource(OCResourceHandle collectionHandle,
287 OCResourceHandle resourceHandle)
289 return result_guard(OCUnBindResource(ref(collectionHandle), ref(resourceHandle)));
292 OCStackResult OCPlatform_impl::unbindResources(const OCResourceHandle collectionHandle,
293 const std::vector<OCResourceHandle>& resourceHandles)
295 for(const auto& h : resourceHandles)
299 if(OC_STACK_OK != (r = result_guard(OCUnBindResource(collectionHandle, h))))
308 OCStackResult OCPlatform_impl::bindResource(const OCResourceHandle collectionHandle,
309 const OCResourceHandle resourceHandle)
311 return result_guard(OCBindResource(collectionHandle, resourceHandle));
314 OCStackResult OCPlatform_impl::bindResources(const OCResourceHandle collectionHandle,
315 const std::vector<OCResourceHandle>& resourceHandles)
317 for(const auto& h : resourceHandles)
321 if(OC_STACK_OK != (r = result_guard(OCBindResource(collectionHandle, h))))
330 OCStackResult OCPlatform_impl::bindTypeToResource(const OCResourceHandle& resourceHandle,
331 const std::string& resourceTypeName) const
333 return checked_guard(m_server, &IServerWrapper::bindTypeToResource,
334 resourceHandle, resourceTypeName);
337 OCStackResult OCPlatform_impl::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
338 const std::string& resourceInterfaceName) const
340 return checked_guard(m_server, &IServerWrapper::bindInterfaceToResource,
341 resourceHandle, resourceInterfaceName);
344 OCStackResult OCPlatform_impl::startPresence(const unsigned int announceDurationSeconds)
346 return checked_guard(m_server, &IServerWrapper::startPresence,
347 announceDurationSeconds);
350 OCStackResult OCPlatform_impl::stopPresence()
352 return checked_guard(m_server, &IServerWrapper::stopPresence);
356 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
357 const std::string& host, uint8_t connectivityType,
358 SubscribeCallback presenceHandler)
360 return subscribePresence(presenceHandle, host, "", connectivityType, presenceHandler);
364 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
365 const std::string& host,
366 const std::string& resourceType,
367 uint8_t connectivityType,
368 SubscribeCallback presenceHandler)
370 return checked_guard(m_client, &IClientWrapper::SubscribePresence,
371 &presenceHandle, host, resourceType, connectivityType,
375 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
376 const std::string& host,
377 SubscribeCallback presenceHandler)
379 return subscribePresence(presenceHandle, host, "", presenceHandler);
382 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
383 const std::string& host,
384 const std::string& resourceType,
385 SubscribeCallback presenceHandler)
387 return checked_guard(m_client, &IClientWrapper::SubscribePresence,
388 &presenceHandle, host, resourceType,
393 OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle presenceHandle)
395 return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
396 ref(presenceHandle));
399 OCStackResult OCPlatform_impl::sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
401 return checked_guard(m_server, &IServerWrapper::sendResponse,