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 <cereal/cereal.hpp>
22 #include <cereal/types/memory.hpp>
23 #include <cereal/types/vector.hpp>
24 #include <cereal/archives/json.hpp>
26 #include <StringConstants.h>
30 class ListenOCContainer
33 enum class OCSecureType
39 class ListenResourceContainer
41 class ListenResourcePolicyContainer
43 friend class cereal::access;
44 friend class ListenResourceContainer;
45 friend class ListenResourcePropertiesContainer;
47 template<class Archive>
48 void serialize(Archive& ar)
53 ar(cereal::make_nvp(OC::Key::BMKEY, m_bm));
54 // In case of observable
55 if(m_bm & OC_OBSERVABLE)
60 catch(cereal::Exception&)
62 ar.setNextName(nullptr);
68 ar(cereal::make_nvp(OC::Key::SECUREKEY, secureTemp));
69 m_secure = secureTemp != 0;
72 ar(cereal::make_nvp(OC::Key::PORTKEY, m_port));
74 catch(cereal::Exception&)
76 ar.setNextName(nullptr);
87 class ListenResourcePropertiesContainer
89 friend class cereal::access;
90 friend class ListenResourceContainer;
92 template<class Archive>
93 void serialize(Archive& ar)
97 ar(cereal::make_nvp(OC::Key::POLICYKEY, m_policy));
100 catch(cereal::Exception&)
102 // we swallow this exception, since it means the key
103 // doesn't exist, allowing these to be optional
104 oclog() << "Invalid POLICYKEY"<<std::flush;
105 ar.setNextName(nullptr);
110 ar(cereal::make_nvp(OC::Key::RESOURCETYPESKEY,m_resourceTypes));
112 catch(cereal::Exception&)
114 ar.setNextName(nullptr);
118 ar(cereal::make_nvp(OC::Key::INTERFACESKEY, m_interfaces));
120 catch(cereal::Exception&)
122 ar.setNextName(nullptr);
126 std::vector<std::string> m_resourceTypes;
127 std::vector<std::string> m_interfaces;
128 ListenResourcePolicyContainer m_policy;
132 ListenResourceContainer() : m_loaded(false)
136 friend class cereal::access;
137 friend class ListenOCContainer;
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);
153 ar(cereal::make_nvp(OC::Key::SERVERIDKEY, m_serverId));
156 catch(cereal::Exception&)
158 ar.setNextName(nullptr);
162 ar(cereal::make_nvp(OC::Key::PROPERTYKEY, m_props));
165 catch(cereal::Exception&)
167 ar.setNextName(nullptr);
173 std::string m_serverId;
175 ListenResourcePropertiesContainer m_props;
182 bool observable() const
184 return m_props.m_policy.m_observable;
187 OCSecureType secureType() const
189 return m_props.m_policy.m_secure?OCSecureType::IPv4Secure :OCSecureType::IPv4;
194 return m_props.m_policy.m_port;
197 std::vector<std::string> resourceTypes() const
199 return m_props.m_resourceTypes;
202 std::vector<std::string> interfaces() const
204 return m_props.m_interfaces;
209 friend class cereal::access;
210 template <class Archive>
211 void serialize(Archive& ar)
213 std::vector<ListenResourceContainer> resources;
217 ListenOCContainer(std::weak_ptr<IClientWrapper> cw, const OCDevAddr& address,
218 OCConnectivityType connectivityType, std::stringstream& json):
219 m_clientWrapper(cw), m_address(address), m_connectivityType(connectivityType)
224 const std::vector<std::shared_ptr<OCResource>>& Resources() const
230 std::string ConvertOCAddrToString(OCSecureType sec, int secureport)
233 std::ostringstream os;
235 if(sec== OCSecureType::IPv4)
239 else if(sec == OCSecureType::IPv4Secure)
245 oclog() << "ConvertOCAddrToString(): invalid SecureType"<<std::flush;
246 throw ResourceInitException(false, false, false, false, false, true);
253 if(OCDevAddrToIPv4Addr(&m_address, &a, &b, &c, &d) != 0)
255 oclog() << "ConvertOCAddrToString(): Invalid Ip"
257 throw ResourceInitException(false, false, false, false, false, true);
260 os<<static_cast<int>(a)<<"."<<static_cast<int>(b)
261 <<"."<<static_cast<int>(c)<<"."<<static_cast<int>(d);
263 if(sec == OCSecureType::IPv4Secure && secureport>0 && secureport<=65535)
265 port = static_cast<uint16_t>(secureport);
267 else if(sec == OCSecureType::IPv4 && 0==OCDevAddrToPort(&m_address, &port))
269 // nothing to do, this is a successful case
273 oclog() << "ConvertOCAddrToString() : Invalid Port"
275 throw ResourceInitException(false, false, false, false, true, false);
278 os <<":"<< static_cast<int>(port);
283 void LoadFromJson(std::stringstream& json)
285 cereal::JSONInputArchive archive(json);
287 std::vector<ListenResourceContainer> resources;
288 archive(cereal::make_nvp(OC::Key::OCKEY, resources));
292 for(const auto& res : resources)
298 m_resources.push_back(std::shared_ptr<OCResource>(
299 new OCResource(m_clientWrapper,
300 ConvertOCAddrToString(res.secureType(),res.port()),
301 res.m_uri, res.m_serverId, m_connectivityType, res.observable(),
302 res.resourceTypes(), res.interfaces())));
306 catch(ResourceInitException& e)
308 oclog() << "listenCallback(): failed to create resource: " << e.what()
313 std::vector<std::shared_ptr<OC::OCResource>> m_resources;
314 std::weak_ptr<IClientWrapper> m_clientWrapper;
316 OCConnectivityType m_connectivityType;