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 const 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)
56 m_resources.push_back(std::shared_ptr<OC::OCResource>(
57 new OC::OCResource(m_clientWrapper, m_devAddr,
58 std::string(res->uri),
59 std::string(uuidString),
60 (res->bitmap & OC_OBSERVABLE) == OC_OBSERVABLE,
61 StringLLToVector(res->types),
62 StringLLToVector(res->interfaces)
69 const std::vector<std::shared_ptr<OCResource>>& Resources() const
74 std::vector<std::shared_ptr<OC::OCResource>> m_resources;
75 std::weak_ptr<IClientWrapper> m_clientWrapper;
76 const OCDevAddr& m_devAddr;
79 class ListenOCContainer
82 enum class OCSecureType
88 class DiscoveredResources
92 friend class cereal::access;
96 friend class cereal::access;
97 friend class Resource;
99 template<class Archive>
100 void serialize(Archive& ar)
104 m_observable = false;
105 ar(cereal::make_nvp(OC::Key::BMKEY, m_bm));
107 if(m_bm & OC_OBSERVABLE)
112 catch(cereal::Exception&)
114 ar.setNextName(nullptr);
121 ar(cereal::make_nvp(OC::Key::SECUREKEY, secureTemp));
122 m_secure = secureTemp != 0;
125 ar(cereal::make_nvp(OC::Key::PORTKEY, m_port));
127 catch(cereal::Exception&)
129 ar.setNextName(nullptr);
139 template <class Archive>
140 void serialize(Archive& ar)
144 ar(cereal::make_nvp(OC::Key::URIKEY, m_uri));
147 catch(cereal::Exception&)
149 ar.setNextName(nullptr);
154 ar(cereal::make_nvp(OC::Key::RESOURCETYPESKEY,m_resourceTypes));
157 catch(cereal::Exception&)
159 ar.setNextName(nullptr);
164 ar(cereal::make_nvp(OC::Key::INTERFACESKEY, m_interfaces));
167 catch(cereal::Exception&)
169 ar.setNextName(nullptr);
174 ar(cereal::make_nvp(OC::Key::POLICYKEY, m_policy));
177 catch(cereal::Exception&)
179 ar.setNextName(nullptr);
182 // Although not expected, a server id as part of a resource's own
183 // representation is legal. It may be used if needed.
186 ar(cereal::make_nvp(OC::Key::DEVICEIDKEY, m_serverId));
189 catch(cereal::Exception&)
191 ar.setNextName(nullptr);
195 Resource(): m_loaded(false)
198 bool observable() const
200 return m_policy.m_observable;
203 OCSecureType secureType() const
205 return m_policy.m_secure ? OCSecureType::Secure : OCSecureType::NotSecure;
210 return m_policy.m_port;
213 std::vector<std::string> resourceTypes() const
215 return m_resourceTypes;
218 std::vector<std::string> interfaces() const
228 std::string m_serverId;
229 std::vector<std::string> m_resourceTypes;
230 std::vector<std::string> m_interfaces;
231 ResourcePolicy m_policy;
236 DiscoveredResources()
240 friend class cereal::access;
241 friend class ListenOCContainer;
243 template <class Archive>
244 void serialize(Archive& ar)
248 ar(cereal::make_nvp(OC::Key::DEVICEIDKEY, m_serverIdOfThisDevice));
250 catch(cereal::Exception&) { ar.setNextName(nullptr); }
254 ar(cereal::make_nvp(OC::Key::LINKS, resources));
256 catch(cereal::Exception&) { ar.setNextName(nullptr); }
259 std::string m_serverIdOfThisDevice;
260 std::vector<Resource> resources;
264 friend class cereal::access;
265 template <class Archive>
266 void serialize(Archive& ar)
268 std::vector<DiscoveredResources> resources;
272 ListenOCContainer(std::weak_ptr<IClientWrapper> cw,
273 const OCDevAddr& devAddr, std::stringstream& json)
274 : m_clientWrapper(cw), m_devAddr(devAddr)
280 const std::vector<std::shared_ptr<OCResource>>& Resources() const
286 void LoadFromJson(std::stringstream& json)
288 cereal::JSONInputArchive archive(json);
290 std::vector<DiscoveredResources> resources;
291 archive(cereal::make_nvp(OC::Key::OCKEY, resources));
295 for(const auto& resourcesAtDevice : resources)
297 std::string serverIDForThisResourceRep = resourcesAtDevice.m_serverIdOfThisDevice;
299 for (const auto& resource : resourcesAtDevice.resources)
303 if(resource.loaded())
305 m_resources.push_back(std::shared_ptr<OCResource>
312 serverIDForThisResourceRep,
313 resource.observable(),
314 resource.resourceTypes(),
315 resource.interfaces()
320 catch(ResourceInitException& e)
322 oclog() << "listenCallback(): failed to create resource: " << e.what()