Imported Upstream version 0.9.2
[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                     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                     if (res->secure)
57                     {
58                         m_devAddr.flags =
59                               (OCTransportFlags)(OC_FLAG_SECURE | m_devAddr.flags);
60                     }
61  
62                     if (res->port != 0)
63                     {
64                          m_devAddr.port = res->port;
65                     }
66
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)
74                                     )));
75                     res = res->next;
76                 }
77
78             }
79
80             const std::vector<std::shared_ptr<OCResource>>& Resources() const
81             {
82                 return m_resources;
83             }
84         private:
85             std::vector<std::shared_ptr<OC::OCResource>> m_resources;
86             std::weak_ptr<IClientWrapper> m_clientWrapper;
87             OCDevAddr& m_devAddr;
88     };
89 }