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 #include <StringConstants.h>
22 #include "ocpayload.h"
27 class ListenOCContainer
30 static std::vector<std::string> StringLLToVector(OCStringLL* ll)
32 std::vector<std::string> strs;
35 strs.push_back(ll->value);
42 ListenOCContainer(std::weak_ptr<IClientWrapper> cw,
43 OCDevAddr& devAddr, OCDiscoveryPayload* payload)
44 : m_clientWrapper(cw), m_devAddr(devAddr)
46 OCResourcePayload* res = payload->resources;
50 char uuidString[UUID_STRING_SIZE];
51 if(OCConvertUuidToString(res->sid, uuidString) != RAND_UUID_OK)
59 (OCTransportFlags)(OC_FLAG_SECURE | m_devAddr.flags);
64 m_devAddr.port = res->port;
67 m_resources.push_back(std::shared_ptr<OC::OCResource>(
68 new OC::OCResource(m_clientWrapper, m_devAddr,
69 std::string(res->uri),
70 std::string(uuidString),
71 (res->bitmap & OC_OBSERVABLE) == OC_OBSERVABLE,
72 StringLLToVector(res->types),
73 StringLLToVector(res->interfaces)
80 const std::vector<std::shared_ptr<OCResource>>& Resources() const
85 std::vector<std::shared_ptr<OC::OCResource>> m_resources;
86 std::weak_ptr<IClientWrapper> m_clientWrapper;
90 class ListenOCContainer
93 enum class OCSecureType
99 class DiscoveredResources
103 friend class cereal::access;
107 friend class cereal::access;
108 friend class Resource;
110 template<class Archive>
111 void serialize(Archive& ar)
115 m_observable = false;
116 ar(cereal::make_nvp(OC::Key::BMKEY, m_bm));
118 if(m_bm & OC_OBSERVABLE)
123 catch(cereal::Exception&)
125 ar.setNextName(nullptr);
132 ar(cereal::make_nvp(OC::Key::SECUREKEY, secureTemp));
133 m_secure = secureTemp != 0;
136 ar(cereal::make_nvp(OC::Key::PORTKEY, m_port));
138 catch(cereal::Exception&)
140 ar.setNextName(nullptr);
150 template <class Archive>
151 void serialize(Archive& ar)
155 ar(cereal::make_nvp(OC::Key::URIKEY, m_uri));
158 catch(cereal::Exception&)
160 ar.setNextName(nullptr);
165 ar(cereal::make_nvp(OC::Key::RESOURCETYPESKEY,m_resourceTypes));
168 catch(cereal::Exception&)
170 ar.setNextName(nullptr);
175 ar(cereal::make_nvp(OC::Key::INTERFACESKEY, m_interfaces));
178 catch(cereal::Exception&)
180 ar.setNextName(nullptr);
185 ar(cereal::make_nvp(OC::Key::POLICYKEY, m_policy));
188 catch(cereal::Exception&)
190 ar.setNextName(nullptr);
193 // Although not expected, a server id as part of a resource's own
194 // representation is legal. It may be used if needed.
197 ar(cereal::make_nvp(OC::Key::DEVICEIDKEY, m_serverId));
200 catch(cereal::Exception&)
202 ar.setNextName(nullptr);
206 Resource(): m_loaded(false)
209 bool observable() const
211 return m_policy.m_observable;
214 OCSecureType secureType() const
216 return m_policy.m_secure ? OCSecureType::Secure : OCSecureType::NotSecure;
221 return m_policy.m_port;
224 std::vector<std::string> resourceTypes() const
226 return m_resourceTypes;
229 std::vector<std::string> interfaces() const
239 std::string m_serverId;
240 std::vector<std::string> m_resourceTypes;
241 std::vector<std::string> m_interfaces;
242 ResourcePolicy m_policy;
247 DiscoveredResources()
251 friend class cereal::access;
252 friend class ListenOCContainer;
254 template <class Archive>
255 void serialize(Archive& ar)
259 ar(cereal::make_nvp(OC::Key::DEVICEIDKEY, m_serverIdOfThisDevice));
261 catch(cereal::Exception&) { ar.setNextName(nullptr); }
265 ar(cereal::make_nvp(OC::Key::LINKS, resources));
267 catch(cereal::Exception&) { ar.setNextName(nullptr); }
270 std::string m_serverIdOfThisDevice;
271 std::vector<Resource> resources;
275 friend class cereal::access;
276 template <class Archive>
277 void serialize(Archive& ar)
279 std::vector<DiscoveredResources> resources;
283 ListenOCContainer(std::weak_ptr<IClientWrapper> cw,
284 const OCDevAddr& devAddr, std::stringstream& json)
285 : m_clientWrapper(cw), m_devAddr(devAddr)
291 const std::vector<std::shared_ptr<OCResource>>& Resources() const
297 void LoadFromJson(std::stringstream& json)
299 cereal::JSONInputArchive archive(json);
301 std::vector<DiscoveredResources> resources;
302 archive(cereal::make_nvp(OC::Key::OCKEY, resources));
306 for(const auto& resourcesAtDevice : resources)
308 std::string serverIDForThisResourceRep = resourcesAtDevice.m_serverIdOfThisDevice;
310 for (const auto& resource : resourcesAtDevice.resources)
314 if(resource.loaded())
316 m_resources.push_back(std::shared_ptr<OCResource>
323 serverIDForThisResourceRep,
324 resource.observable(),
325 resource.resourceTypes(),
326 resource.interfaces()
331 catch(ResourceInitException& e)
333 oclog() << "listenCallback(): failed to create resource: " << e.what()