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 //******************************************************************
25 // Description: Implementation of the OCPlatform.
29 //*********************************************************************
37 #include "OCPlatform.h"
39 #include "OCException.h"
40 #include "OCUtilities.h"
42 #include "oc_logger.hpp"
46 void OCPlatform::init(const PlatformConfig& config)
50 case ModeType::Server:
51 m_server = m_WrapperInstance->CreateServerWrapper(*this, m_csdkLock, config);
54 case ModeType::Client:
55 m_client = m_WrapperInstance->CreateClientWrapper(*this, m_csdkLock, config);
59 m_server = m_WrapperInstance->CreateServerWrapper(*this, m_csdkLock, config);
60 m_client = m_WrapperInstance->CreateClientWrapper(*this, m_csdkLock, config);
65 OCPlatform::OCPlatform(const PlatformConfig& config)
67 m_WrapperInstance { make_unique<WrapperFactory>() },
68 m_csdkLock { make_shared<std::recursive_mutex>() }
73 OCPlatform::~OCPlatform(void)
77 OCStackResult OCPlatform::setDefaultDeviceEntityHandler(EntityHandler entityHandler)
79 return checked_guard(m_server, &IServerWrapper::setDefaultDeviceEntityHandler,
83 OCStackResult OCPlatform::notifyAllObservers(OCResourceHandle resourceHandle, QualityOfService QoS)
85 return result_guard(OCNotifyAllObservers(resourceHandle, static_cast<OCQualityOfService>(QoS)));
88 OCStackResult OCPlatform::notifyAllObservers(OCResourceHandle resourceHandle)
90 return notifyAllObservers(resourceHandle, m_cfg.QoS);
93 OCStackResult OCPlatform::notifyListOfObservers(OCResourceHandle resourceHandle,
94 ObservationIds& observationIds,
95 const std::shared_ptr<OCResourceResponse> pResponse)
97 return notifyListOfObservers(resourceHandle, observationIds, pResponse, m_cfg.QoS);
100 OCStackResult OCPlatform::notifyListOfObservers(OCResourceHandle resourceHandle,
101 ObservationIds& observationIds,
102 const std::shared_ptr<OCResourceResponse> pResponse,
103 QualityOfService QoS)
107 return result_guard(OC_STACK_ERROR);
110 std::string payload(pResponse->getPayload());
113 OCNotifyListOfObservers(resourceHandle,
114 &observationIds[0], observationIds.size(),
115 reinterpret_cast<unsigned char *>(const_cast<char *>(payload.c_str())),
116 static_cast<OCQualityOfService>(QoS)));
119 OCResource::Ptr OCPlatform::constructResourceObject(const std::string& host, const std::string& uri,
120 bool isObservable, const std::vector<std::string>& resourceTypes,
121 const std::vector<std::string>& interfaces)
125 return std::shared_ptr<OCResource>();
128 return std::shared_ptr<OCResource>(new OCResource(m_client, host, uri, isObservable, resourceTypes, interfaces));
131 OCStackResult OCPlatform::findResource(const std::string& host, const std::string& resourceName,
132 FindCallback resourceHandler)
134 return findResource(host, resourceName, resourceHandler, m_cfg.QoS);
137 OCStackResult OCPlatform::findResource(const std::string& host, const std::string& resourceName,
138 FindCallback resourceHandler, QualityOfService QoS)
140 return checked_guard(m_client, &IClientWrapper::ListenForResource,
141 host, resourceName, resourceHandler, QoS);
145 OCStackResult OCPlatform::registerResource(OCResourceHandle& resourceHandle,
146 std::string& resourceURI,
147 const std::string& resourceTypeName,
148 const std::string& resourceInterface,
149 EntityHandler entityHandler,
150 uint8_t resourceProperty)
152 return checked_guard(m_server, &IServerWrapper::registerResource,
153 ref(resourceHandle), resourceURI, resourceTypeName,
154 resourceInterface, entityHandler, resourceProperty);
157 OCStackResult OCPlatform::unregisterResource(const OCResourceHandle& resourceHandle) const
159 return checked_guard(m_server, &IServerWrapper::unregisterResource,
163 OCStackResult OCPlatform::unbindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle)
165 return result_guard(OCUnBindResource(ref(collectionHandle), ref(resourceHandle)));
168 OCStackResult OCPlatform::unbindResources(const OCResourceHandle collectionHandle, const std::vector<OCResourceHandle>& resourceHandles)
170 for(const auto& h : resourceHandles)
174 if(OC_STACK_OK != (r = result_guard(OCUnBindResource(collectionHandle, h))))
183 OCStackResult OCPlatform::bindResource(const OCResourceHandle collectionHandle, const OCResourceHandle resourceHandle)
185 return result_guard(OCBindResource(collectionHandle, resourceHandle));
188 OCStackResult OCPlatform::bindResources(const OCResourceHandle collectionHandle, const std::vector<OCResourceHandle>& resourceHandles)
190 for(const auto& h : resourceHandles)
194 if(OC_STACK_OK != (r = result_guard(OCBindResource(collectionHandle, h))))
203 OCStackResult OCPlatform::bindTypeToResource(const OCResourceHandle& resourceHandle,
204 const std::string& resourceTypeName) const
206 return checked_guard(m_server, &IServerWrapper::bindTypeToResource,
207 resourceHandle, resourceTypeName);
210 OCStackResult OCPlatform::bindInterfaceToResource(const OCResourceHandle& resourceHandle,
211 const std::string& resourceInterfaceName) const
213 return checked_guard(m_server, &IServerWrapper::bindInterfaceToResource,
214 resourceHandle, resourceInterfaceName);
217 OCStackResult OCPlatform::startPresence(const unsigned int announceDurationSeconds)
219 return checked_guard(m_server, &IServerWrapper::startPresence,
220 announceDurationSeconds);
223 OCStackResult OCPlatform::stopPresence()
225 return checked_guard(m_server, &IServerWrapper::stopPresence);
228 OCStackResult OCPlatform::subscribePresence(OCPresenceHandle& presenceHandle, const std::string& host,
229 SubscribeCallback presenceHandler)
231 return checked_guard(m_client, &IClientWrapper::SubscribePresence,
232 &presenceHandle, host, presenceHandler);
235 OCStackResult OCPlatform::unsubscribePresence(OCPresenceHandle presenceHandle)
237 return checked_guard(m_client, &IClientWrapper::UnsubscribePresence,
238 ref(presenceHandle));