CBOR rebase onto master for merge/review
[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 <StringConstants.h>
22 #include "ocpayload.h"
23 #include "ocrandom.h"
24
25 namespace OC
26 {
27     class ListenOCContainer
28     {
29         private:
30             static std::vector<std::string> StringLLToVector(OCStringLL* ll)
31             {
32                 std::vector<std::string> strs;
33                 while(ll)
34                 {
35                     strs.push_back(ll->value);
36                     ll = ll->next;
37                 }
38                 return strs;
39             }
40
41         public:
42             ListenOCContainer(std::weak_ptr<IClientWrapper> cw,
43                     const OCDevAddr& devAddr, OCDiscoveryPayload* payload)
44                     : m_clientWrapper(cw), m_devAddr(devAddr)
45             {
46                 OCResourcePayload* res = payload->resources;
47
48                 while(res)
49                 {
50                     char uuidString[UUID_STRING_SIZE];
51                     if(OCConvertUuidToString(res->sid, uuidString) != RAND_UUID_OK)
52                     {
53                         uuidString[0]= '\0';
54                     }
55
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)
63                                     )));
64                     res = res->next;
65                 }
66
67             }
68
69             const std::vector<std::shared_ptr<OCResource>>& Resources() const
70             {
71                 return m_resources;
72             }
73         private:
74             std::vector<std::shared_ptr<OC::OCResource>> m_resources;
75             std::weak_ptr<IClientWrapper> m_clientWrapper;
76             const OCDevAddr& m_devAddr;
77     };
78     /*
79     class ListenOCContainer
80     {
81         private:
82         enum class OCSecureType
83         {
84             NotSecure,
85             Secure
86         };
87
88         class DiscoveredResources
89         {
90             class Resource
91             {
92                 friend class cereal::access;
93
94                 class ResourcePolicy
95                 {
96                     friend class cereal::access;
97                     friend class Resource;
98
99                     template<class Archive>
100                     void serialize(Archive& ar)
101                     {
102                         try
103                         {
104                             m_observable = false;
105                             ar(cereal::make_nvp(OC::Key::BMKEY, m_bm));
106
107                             if(m_bm & OC_OBSERVABLE)
108                             {
109                                 m_observable = true;
110                             }
111                         }
112                         catch(cereal::Exception&)
113                         {
114                             ar.setNextName(nullptr);
115                         }
116
117                         try
118                         {
119                             m_secure = false;
120                             int secureTemp;
121                             ar(cereal::make_nvp(OC::Key::SECUREKEY, secureTemp));
122                             m_secure = secureTemp != 0;
123
124                             m_port = -1;
125                             ar(cereal::make_nvp(OC::Key::PORTKEY, m_port));
126                         }
127                         catch(cereal::Exception&)
128                         {
129                             ar.setNextName(nullptr);
130                         }
131                      }
132
133                      bool m_observable;
134                      uint8_t m_bm;
135                      bool m_secure;
136                      int m_port;
137                 };
138
139                 template <class Archive>
140                 void serialize(Archive& ar)
141                 {
142                     try
143                     {
144                         ar(cereal::make_nvp(OC::Key::URIKEY, m_uri));
145                         m_loaded = true;
146                     }
147                     catch(cereal::Exception&)
148                     {
149                         ar.setNextName(nullptr);
150                     }
151
152                     try
153                     {
154                         ar(cereal::make_nvp(OC::Key::RESOURCETYPESKEY,m_resourceTypes));
155                         m_loaded = true;
156                     }
157                     catch(cereal::Exception&)
158                     {
159                         ar.setNextName(nullptr);
160                     }
161
162                     try
163                     {
164                         ar(cereal::make_nvp(OC::Key::INTERFACESKEY, m_interfaces));
165                         m_loaded = true;
166                     }
167                     catch(cereal::Exception&)
168                     {
169                         ar.setNextName(nullptr);
170                     }
171
172                     try
173                     {
174                         ar(cereal::make_nvp(OC::Key::POLICYKEY, m_policy));
175                         m_loaded = true;
176                     }
177                     catch(cereal::Exception&)
178                     {
179                         ar.setNextName(nullptr);
180                     }
181
182                     // Although not expected, a server id as part of a resource's own
183                     // representation is legal. It may be used if needed.
184                     try
185                     {
186                         ar(cereal::make_nvp(OC::Key::DEVICEIDKEY, m_serverId));
187                         m_loaded = true;
188                     }
189                     catch(cereal::Exception&)
190                     {
191                         ar.setNextName(nullptr);
192                     }
193                 }
194             public:
195                 Resource(): m_loaded(false)
196                 {}
197
198                 bool observable() const
199                 {
200                     return m_policy.m_observable;
201                 }
202
203                 OCSecureType secureType() const
204                 {
205                     return m_policy.m_secure ? OCSecureType::Secure : OCSecureType::NotSecure;
206                 }
207
208                 int port() const
209                 {
210                     return m_policy.m_port;
211                 }
212
213                 std::vector<std::string> resourceTypes() const
214                 {
215                     return m_resourceTypes;
216                 }
217
218                 std::vector<std::string> interfaces() const
219                 {
220                     return m_interfaces;
221                 }
222
223                 bool loaded() const{
224                     return m_loaded;
225                 }
226
227                 std::string m_uri;
228                 std::string m_serverId;
229                 std::vector<std::string> m_resourceTypes;
230                 std::vector<std::string> m_interfaces;
231                 ResourcePolicy m_policy;
232                 bool m_loaded;
233             };
234
235             public:
236             DiscoveredResources()
237             {}
238
239             private:
240             friend class cereal::access;
241             friend class ListenOCContainer;
242
243             template <class Archive>
244             void serialize(Archive& ar)
245             {
246                 try
247                 {
248                     ar(cereal::make_nvp(OC::Key::DEVICEIDKEY, m_serverIdOfThisDevice));
249                 }
250                 catch(cereal::Exception&) { ar.setNextName(nullptr); }
251
252                 try
253                 {
254                     ar(cereal::make_nvp(OC::Key::LINKS, resources));
255                 }
256                 catch(cereal::Exception&) { ar.setNextName(nullptr); }
257             }
258
259             std::string m_serverIdOfThisDevice;
260             std::vector<Resource> resources;
261         };
262
263         private:
264             friend class cereal::access;
265             template <class Archive>
266             void serialize(Archive& ar)
267             {
268                 std::vector<DiscoveredResources> resources;
269                 ar(resources);
270             }
271         public:
272             ListenOCContainer(std::weak_ptr<IClientWrapper> cw,
273                     const OCDevAddr& devAddr, std::stringstream& json)
274                     : m_clientWrapper(cw), m_devAddr(devAddr)
275
276             {
277                 LoadFromJson(json);
278             }
279
280             const std::vector<std::shared_ptr<OCResource>>& Resources() const
281             {
282                 return m_resources;
283             }
284
285         private:
286             void LoadFromJson(std::stringstream& json)
287             {
288                 cereal::JSONInputArchive archive(json);
289
290                 std::vector<DiscoveredResources> resources;
291                 archive(cereal::make_nvp(OC::Key::OCKEY, resources));
292
293                 m_resources.clear();
294
295                 for(const auto& resourcesAtDevice : resources)
296                 {
297                     std::string serverIDForThisResourceRep = resourcesAtDevice.m_serverIdOfThisDevice;
298
299                     for (const auto& resource : resourcesAtDevice.resources)
300                     {
301                         try
302                         {
303                             if(resource.loaded())
304                             {
305                                 m_resources.push_back(std::shared_ptr<OCResource>
306                                 (
307                                     new OCResource
308                                     (
309                                         m_clientWrapper,
310                                         m_devAddr,
311                                         resource.m_uri,
312                                         serverIDForThisResourceRep,
313                                         resource.observable(),
314                                         resource.resourceTypes(),
315                                         resource.interfaces()
316                                     )
317                                 ));
318                             }
319                         }
320                         catch(ResourceInitException& e)
321                         {
322                             oclog() << "listenCallback(): failed to create resource: " << e.what()
323                                     << std::flush;
324                         }
325                     }
326                 }
327             }
328     };
329 */
330 }