Merge branch 'master' into easysetup
[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                 OCResourceCollectionPayload* colRes = payload->collectionResources;
48                 if (res)
49                 {
50                     while(res)
51                     {
52                         char uuidString[UUID_STRING_SIZE];
53                         if(OCConvertUuidToString(res->sid, uuidString) != RAND_UUID_OK)
54                         {
55                             uuidString[0]= '\0';
56                         }
57
58                         if (res->secure)
59                         {
60                             m_devAddr.flags =
61                                   (OCTransportFlags)(OC_FLAG_SECURE | m_devAddr.flags);
62                         }
63
64                         if (res->port != 0)
65                         {
66                              m_devAddr.port = res->port;
67                         }
68
69                         m_resources.push_back(std::shared_ptr<OC::OCResource>(
70                                     new OC::OCResource(m_clientWrapper, m_devAddr,
71                                         std::string(res->uri),
72                                         std::string(uuidString),
73                                         (res->bitmap & OC_OBSERVABLE) == OC_OBSERVABLE,
74                                         StringLLToVector(res->types),
75                                         StringLLToVector(res->interfaces)
76                                         )));
77                         res = res->next;
78                     }
79                 }
80                 else if (colRes)
81                 {
82                     while(colRes)
83                     {
84                         if (colRes->tags->bitmap & OC_SECURE)
85                         {
86                             m_devAddr.flags =
87                                   (OCTransportFlags)(OC_FLAG_SECURE | m_devAddr.flags);
88                         }
89
90                         if (colRes->tags->port != 0)
91                         {
92                              m_devAddr.port = colRes->tags->port;
93                         }
94
95                         m_resources.push_back(std::shared_ptr<OC::OCResource>(
96                                     new OC::OCResource(m_clientWrapper, m_devAddr,
97                                         std::string(colRes->setLinks->href),
98                                         std::string((char*)colRes->tags->di.id),
99                                         (colRes->tags->bitmap & OC_OBSERVABLE) == OC_OBSERVABLE,
100                                         StringLLToVector(colRes->setLinks->rt),
101                                         StringLLToVector(colRes->setLinks->itf)
102                                         )));
103                         colRes = colRes->next;
104                     }
105                 }
106             }
107
108             const std::vector<std::shared_ptr<OCResource>>& Resources() const
109             {
110                 return m_resources;
111             }
112         private:
113             std::vector<std::shared_ptr<OC::OCResource>> m_resources;
114             std::weak_ptr<IClientWrapper> m_clientWrapper;
115             OCDevAddr& m_devAddr;
116     };
117 }