Merge branch 'connectivity-abstraction' to master
[platform/upstream/iotivity.git] / resource / include / OCSerialization.h
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <cereal/cereal.hpp>
22 #include <cereal/types/memory.hpp>
23 #include <cereal/types/vector.hpp>
24 #include <cereal/archives/json.hpp>
25
26 #include <StringConstants.h>
27
28 namespace OC
29 {
30     class ListenOCContainer
31     {
32         private:
33         enum class OCSecureType
34         {
35             IPv4Secure,
36             IPv4
37         };
38
39         class ListenResourceContainer
40         {
41             class ListenResourcePropertiesContainer
42             {
43                 friend class cereal::access;
44                 friend class ListenResourceContainer;
45
46                 template<class Archive>
47                 void serialize(Archive& ar)
48                 {
49                     try
50                     {
51                         m_observable=false;
52                         int obsTemp;
53                         ar(cereal::make_nvp(OC::Key::OBSERVABLEKEY, obsTemp));
54                         m_observable = obsTemp != 0;
55                     }
56                     catch(cereal::Exception&)
57                     {
58                         // we swallow this exception, since it means the key
59                         // doesn't exist, allowing these to be optional
60                         ar.setNextName(nullptr);
61                     }
62
63                     try
64                     {
65                         m_secure = false;
66                         int secureTemp;
67                         ar(cereal::make_nvp(OC::Key::SECUREKEY, secureTemp));
68                         m_secure = secureTemp != 0;
69
70                         m_port = -1;
71                         ar(cereal::make_nvp(OC::Key::PORTKEY, m_port));
72                     }
73                     catch(cereal::Exception&)
74                     {
75                         ar.setNextName(nullptr);
76                     }
77
78                     try
79                     {
80                         ar(cereal::make_nvp(OC::Key::RESOURCETYPESKEY,m_resourceTypes));
81                     }
82                     catch(cereal::Exception&)
83                     {
84                         ar.setNextName(nullptr);
85                     }
86                     try
87                     {
88                         ar(cereal::make_nvp(OC::Key::INTERFACESKEY, m_interfaces));
89                     }
90                     catch(cereal::Exception&)
91                     {
92                         ar.setNextName(nullptr);
93                     }
94                 }
95
96                 bool m_observable;
97                 std::vector<std::string> m_resourceTypes;
98                 std::vector<std::string> m_interfaces;
99                 bool m_secure;
100                 int m_port;
101             };
102
103             public:
104             ListenResourceContainer() : m_loaded(false)
105             {}
106
107             private:
108             friend class cereal::access;
109             friend class ListenOCContainer;
110
111             template <class Archive>
112             void serialize(Archive& ar)
113             {
114                 try
115                 {
116                     ar(cereal::make_nvp(OC::Key::URIKEY, m_uri));
117                     m_loaded=true;
118                 }
119                 catch(cereal::Exception&)
120                 {
121                     ar.setNextName(nullptr);
122                 }
123                 try
124                 {
125                     ar(cereal::make_nvp(OC::Key::SERVERIDKEY, m_serverId));
126                     m_loaded=true;
127                 }
128                 catch(cereal::Exception&)
129                 {
130                     ar.setNextName(nullptr);
131                 }
132                 try
133                 {
134                     ar(cereal::make_nvp(OC::Key::PROPERTYKEY, m_props));
135                     m_loaded=true;
136                 }
137                 catch(cereal::Exception&)
138                 {
139                     ar.setNextName(nullptr);
140                 }
141             }
142
143
144             std::string m_uri;
145             std::string m_serverId;
146             bool m_loaded;
147             ListenResourcePropertiesContainer m_props;
148
149             bool loaded() const
150             {
151                 return m_loaded;
152             }
153
154             bool observable() const
155             {
156                 return m_props.m_observable;
157             }
158
159             OCSecureType secureType() const
160             {
161                 return m_props.m_secure?OCSecureType::IPv4Secure :OCSecureType::IPv4;
162             }
163
164             int port() const
165             {
166                 return m_props.m_port;
167             }
168
169             std::vector<std::string> resourceTypes() const
170             {
171                 return m_props.m_resourceTypes;
172             }
173
174             std::vector<std::string> interfaces() const
175             {
176                 return m_props.m_interfaces;
177             }
178         };
179
180         private:
181             friend class cereal::access;
182             template <class Archive>
183             void serialize(Archive& ar)
184             {
185                 std::vector<ListenResourceContainer> resources;
186                 ar(resources);
187             }
188         public:
189             ListenOCContainer(std::weak_ptr<IClientWrapper> cw, const OCDevAddr& address,
190                     OCConnectivityType connectivityType, std::stringstream& json):
191                 m_clientWrapper(cw), m_address(address), m_connectivityType(connectivityType)
192             {
193                 LoadFromJson(json);
194             }
195
196             const std::vector<std::shared_ptr<OCResource>>& Resources() const
197             {
198                 return m_resources;
199             }
200
201         private:
202             std::string ConvertOCAddrToString(OCSecureType sec, int secureport)
203             {
204                 uint16_t port;
205                 std::ostringstream os;
206
207                 if(sec== OCSecureType::IPv4)
208                 {
209                     os<<"coap://";
210                 }
211                 else if(sec == OCSecureType::IPv4Secure)
212                 {
213                     os<<"coaps://";
214                 }
215                 else
216                 {
217                     oclog() << "ConvertOCAddrToString():  invalid SecureType"<<std::flush;
218                     throw ResourceInitException(false, false, false, false, false, true);
219                 }
220
221                 uint8_t a;
222                 uint8_t b;
223                 uint8_t c;
224                 uint8_t d;
225                 if(OCDevAddrToIPv4Addr(&m_address, &a, &b, &c, &d) != 0)
226                 {
227                     oclog() << "ConvertOCAddrToString(): Invalid Ip"
228                             << std::flush;
229                     throw ResourceInitException(false, false, false, false, false, true);
230                 }
231
232                 os<<static_cast<int>(a)<<"."<<static_cast<int>(b)
233                         <<"."<<static_cast<int>(c)<<"."<<static_cast<int>(d);
234
235                 if(sec == OCSecureType::IPv4Secure && secureport>0 && secureport<=65535)
236                 {
237                     port = static_cast<uint16_t>(secureport);
238                 }
239                 else if(sec == OCSecureType::IPv4 && 0==OCDevAddrToPort(&m_address, &port))
240                 {
241                     // nothing to do, this is a successful case
242                 }
243                 else
244                 {
245                     oclog() << "ConvertOCAddrToString() : Invalid Port"
246                             <<std::flush;
247                     throw ResourceInitException(false, false, false, false, true, false);
248                 }
249
250                 os <<":"<< static_cast<int>(port);
251
252                 return os.str();
253             }
254
255             void LoadFromJson(std::stringstream& json)
256             {
257                 cereal::JSONInputArchive archive(json);
258
259                 std::vector<ListenResourceContainer> resources;
260                 archive(cereal::make_nvp(OC::Key::OCKEY, resources));
261
262                 m_resources.clear();
263
264                 for(const auto& res : resources)
265                 {
266                     try
267                     {
268                         if(res.loaded())
269                         {
270                             m_resources.push_back(std::shared_ptr<OCResource>(
271                                 new OCResource(m_clientWrapper,
272                                     ConvertOCAddrToString(res.secureType(),res.port()),
273                                     res.m_uri, res.m_serverId, m_connectivityType, res.observable(),
274                                     res.resourceTypes(), res.interfaces())));
275                         }
276
277                     }
278                     catch(ResourceInitException& e)
279                     {
280                         oclog() << "listenCallback(): failed to create resource: " << e.what()
281                                 << std::flush;
282                     }
283                 }
284             }
285             std::vector<std::shared_ptr<OC::OCResource>> m_resources;
286             std::weak_ptr<IClientWrapper> m_clientWrapper;
287             OCDevAddr m_address;
288             OCConnectivityType m_connectivityType;
289     };
290 }
291