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 OCRegisterPersistentStorageHandler(config.ps);
61 globalConfig() = config;
64 OCPlatform_impl& OCPlatform_impl::Instance()
66 static OCPlatform_impl platform(globalConfig());
70 void OCPlatform_impl::init(const PlatformConfig& config)
74 case ModeType::Server:
75 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
78 case ModeType::Client:
79 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
83 m_server = m_WrapperInstance->CreateServerWrapper(m_csdkLock, config);
84 m_client = m_WrapperInstance->CreateClientWrapper(m_csdkLock, config);
89 OCPlatform_impl::OCPlatform_impl(const PlatformConfig& config)
91 m_WrapperInstance { make_unique<WrapperFactory>() },
92 m_csdkLock { std::make_shared<std::recursive_mutex>() }
97 OCPlatform_impl::~OCPlatform_impl(void)
101 OCStackResult OCPlatform_impl::setDefaultDeviceEntityHandler(EntityHandler entityHandler)
103 return checked_guard(m_server, &IServerWrapper::setDefaultDeviceEntityHandler,
107 OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle,
108 QualityOfService QoS)
110 return result_guard(OCNotifyAllObservers(resourceHandle,
111 static_cast<OCQualityOfService>(QoS)));
114 OCStackResult OCPlatform_impl::notifyAllObservers(OCResourceHandle resourceHandle)
116 return notifyAllObservers(resourceHandle, m_cfg.QoS);
119 OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
120 ObservationIds& observationIds,
121 const std::shared_ptr<OCResourceResponse> pResponse)
123 return notifyListOfObservers(resourceHandle, observationIds, pResponse, m_cfg.QoS);
126 OCStackResult OCPlatform_impl::notifyListOfObservers(OCResourceHandle resourceHandle,
127 ObservationIds& observationIds,
128 const std::shared_ptr<OCResourceResponse> pResponse,
129 QualityOfService QoS)
133 return result_guard(OC_STACK_ERROR);
136 OCRepPayload* pl = pResponse->getResourceRepresentation().getPayload();
137 OCStackResult result =
138 OCNotifyListOfObservers(resourceHandle,
139 &observationIds[0], observationIds.size(),
141 static_cast<OCQualityOfService>(QoS));
142 OCRepPayloadDestroy(pl);
143 return result_guard(result);
146 OCResource::Ptr OCPlatform_impl::constructResourceObject(const std::string& host,
147 const std::string& uri,
148 OCConnectivityType connectivityType,
150 const std::vector<std::string>& resourceTypes,
151 const std::vector<std::string>& interfaces)
155 return std::shared_ptr<OCResource>();
158 return std::shared_ptr<OCResource>(new OCResource(m_client,
160 uri, "", connectivityType,
166 OCStackResult OCPlatform_impl::findResource(const std::string& host,
167 const std::string& resourceName,
168 OCConnectivityType connectivityType,
169 FindCallback resourceHandler)
171 return findResource(host, resourceName, connectivityType, resourceHandler, m_cfg.QoS);
174 OCStackResult OCPlatform_impl::findResource(const std::string& host,
175 const std::string& resourceName,
176 OCConnectivityType connectivityType,
177 FindCallback resourceHandler,
178 QualityOfService QoS)
180 return checked_guard(m_client, &IClientWrapper::ListenForResource,
181 host, resourceName, connectivityType, resourceHandler, QoS);
184 OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
185 const std::string& deviceURI,
186 OCConnectivityType connectivityType,
187 FindDeviceCallback deviceInfoHandler)
189 return result_guard(getDeviceInfo(host, deviceURI, connectivityType,
190 deviceInfoHandler, m_cfg.QoS));
193 OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
194 const std::string& deviceURI,
195 OCConnectivityType connectivityType,
196 FindDeviceCallback deviceInfoHandler,
197 QualityOfService QoS)
199 return checked_guard(m_client, &IClientWrapper::ListenForDevice,
200 host, deviceURI, connectivityType, deviceInfoHandler, QoS);
203 OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
204 const std::string& platformURI,
205 OCConnectivityType connectivityType,
206 FindPlatformCallback platformInfoHandler)
208 return result_guard(getPlatformInfo(host, platformURI, connectivityType,
209 platformInfoHandler, m_cfg.QoS));
212 OCStackResult OCPlatform_impl::getPlatformInfo(const std::string& host,
213 const std::string& platformURI,
214 OCConnectivityType connectivityType,
215 FindPlatformCallback platformInfoHandler,
216 QualityOfService QoS)
218 return checked_guard(m_client, &IClientWrapper::ListenForDevice,
219 host, platformURI, connectivityType, platformInfoHandler, QoS);
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)
229 return checked_guard(m_server, &IServerWrapper::registerResource,
230 std::ref(resourceHandle), resourceURI, resourceTypeName,
231 resourceInterface, entityHandler, resourceProperty);
234 OCStackResult OCPlatform_impl::registerDeviceInfo(const OCDeviceInfo deviceInfo)
236 return checked_guard(m_server, &IServerWrapper::registerDeviceInfo, deviceInfo);
239 OCStackResult OCPlatform_impl::registerPlatformInfo(const OCPlatformInfo platformInfo)
241 return checked_guard(m_server, &IServerWrapper::registerPlatformInfo, platformInfo);
244 OCStackResult OCPlatform_impl::registerResource(OCResourceHandle& resourceHandle,
245 const std::shared_ptr< OCResource > resource)
247 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
248 std::vector<std::string> resourceTypes = resource->getResourceTypes();
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);
256 OCStackResult OCPlatform_impl::unregisterResource(const OCResourceHandle& resourceHandle) const
258 return checked_guard(m_server, &IServerWrapper::unregisterResource,
262 OCStackResult OCPlatform_impl::unbindResource(OCResourceHandle collectionHandle,
263 OCResourceHandle resourceHandle)
265 return result_guard(OCUnBindResource(std::ref(collectionHandle), std::ref(resourceHandle)));
268 OCStackResult OCPlatform_impl::unbindResources(const OCResourceHandle collectionHandle,
269 const std::vector<OCResourceHandle>& resourceHandles)
271 for(const auto& h : resourceHandles)
275 if(OC_STACK_OK != (r = result_guard(OCUnBindResource(collectionHandle, h))))
284 OCStackResult OCPlatform_impl::bindResource(const OCResourceHandle collectionHandle,
285 const OCResourceHandle resourceHandle)
287 return result_guard(OCBindResource(collectionHandle, resourceHandle));
290 OCStackResult OCPlatform_impl::bindResources(const OCResourceHandle collectionHandle,
291 const std::vector<OCResourceHandle>& resourceHandles)
293 for(const auto& h : resourceHandles)
297 if(OC_STACK_OK != (r = result_guard(OCBindResource(collectionHandle, h))))
306 OCStackResult OCPlatform_impl::bindTypeToResource(const OCResourceHandle& resourceHandle,
307 const std::string& resourceTypeName) const
309 return checked_guard(m_server, &IServerWrapper::bindTypeToResource,
310 resourceHandle, resourceTypeName);
313 OCStackResult OCPlatform_impl::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
314 const std::string& resourceInterfaceName) const
316 return checked_guard(m_server, &IServerWrapper::bindInterfaceToResource,
317 resourceHandle, resourceInterfaceName);
320 OCStackResult OCPlatform_impl::startPresence(const unsigned int announceDurationSeconds)
322 return checked_guard(m_server, &IServerWrapper::startPresence,
323 announceDurationSeconds);
326 OCStackResult OCPlatform_impl::stopPresence()
328 return checked_guard(m_server, &IServerWrapper::stopPresence);
331 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
332 const std::string& host,
333 OCConnectivityType connectivityType,
334 SubscribeCallback presenceHandler)
336 return subscribePresence(presenceHandle, host, "", connectivityType, presenceHandler);
340 OCStackResult OCPlatform_impl::subscribePresence(OCPresenceHandle& presenceHandle,
341 const std::string& host,
342 const std::string& resourceType,
343 OCConnectivityType connectivityType,
344 SubscribeCallback presenceHandler)
346 return checked_guard(m_client, &IClientWrapper::SubscribePresence,
347 &presenceHandle, host, resourceType, connectivityType,
351 OCStackResult OCPlatform_impl::unsubscribePresence(OCPresenceHandle presenceHandle)
353 return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
354 std::ref(presenceHandle));
357 OCStackResult OCPlatform_impl::sendResponse(const std::shared_ptr<OCResourceResponse> pResponse)
359 return checked_guard(m_server, &IServerWrapper::sendResponse,