Add function to save a ACL into local SVR DB
[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 #include "oic_string.h"
25
26 namespace OC
27 {
28     class ListenOCContainer
29     {
30         private:
31             static std::vector<std::string> StringLLToVector(OCStringLL* ll)
32             {
33                 std::vector<std::string> strs;
34                 while(ll)
35                 {
36                     strs.push_back(ll->value);
37                     ll = ll->next;
38                 }
39                 return strs;
40             }
41
42         public:
43             ListenOCContainer(std::weak_ptr<IClientWrapper> cw,
44                     OCDevAddr& devAddr, OCDiscoveryPayload* payload)
45                     : m_clientWrapper(cw), m_devAddr(devAddr)
46             {
47                 while (payload)
48                 {
49                     OCResourcePayload* res = payload->resources;
50                     while (res)
51                     {
52                         if (res->secure)
53                         {
54                             m_devAddr.flags =
55                                   (OCTransportFlags)(OC_FLAG_SECURE | m_devAddr.flags);
56                         }
57
58                         if (res->port != 0)
59                         {
60                             m_devAddr.port = res->port;
61                         }
62
63                         if (payload->baseURI)
64                         {
65                             OCDevAddr rdPubAddr = m_devAddr;
66
67                             std::string baseURI = std::string(payload->baseURI);
68                             size_t len = baseURI.length();
69                             int addressLen = baseURI.find_first_of(":");
70                             std::string ipaddress = baseURI.substr(0, addressLen);
71                             int port = atoi(baseURI.substr(addressLen + 1, len).c_str());
72                             OICStrcpy(rdPubAddr.addr, addressLen + 1, ipaddress.c_str());
73                             rdPubAddr.port = port;
74                             m_resources.push_back(std::shared_ptr<OC::OCResource>(
75                                         new OC::OCResource(m_clientWrapper, rdPubAddr,
76                                             std::string(res->uri),
77                                             std::string(payload->sid),
78                                             res->bitmap,
79                                             StringLLToVector(res->types),
80                                             StringLLToVector(res->interfaces)
81                                             )));
82                         }
83                         else
84                         {
85                             m_resources.push_back(std::shared_ptr<OC::OCResource>(
86                                     new OC::OCResource(m_clientWrapper, m_devAddr,
87                                         std::string(res->uri),
88                                         std::string(payload->sid),
89                                         res->bitmap,
90                                         StringLLToVector(res->types),
91                                         StringLLToVector(res->interfaces)
92                                         )));
93
94 #ifdef TCP_ADAPTER
95                             if (res->tcpPort != 0)
96                             {
97                                 OCDevAddr tcpDevAddr = m_devAddr;
98                                 tcpDevAddr.port = res->tcpPort;
99                                 tcpDevAddr.adapter = OC_ADAPTER_TCP;
100                                 m_resources.push_back(std::shared_ptr<OC::OCResource>(
101                                             new OC::OCResource(m_clientWrapper, tcpDevAddr,
102                                                 std::string(res->uri),
103                                                 std::string(payload->sid),
104                                                 res->bitmap,
105                                                 StringLLToVector(res->types),
106                                                 StringLLToVector(res->interfaces)
107                                                 )));
108                             }
109 #endif
110                         }
111                         res = res->next;
112                     }
113                     payload = payload->next;
114                 }
115             }
116
117 #ifdef WITH_MQ
118             ListenOCContainer(std::weak_ptr<IClientWrapper> cw,
119                                 OCDevAddr& devAddr, OCRepPayload* payload)
120                                 : m_clientWrapper(cw), m_devAddr(devAddr)
121             {
122                 if (payload)
123                 {
124                     char**topicList = nullptr;
125                     size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0};
126                     OCRepPayloadGetStringArray(payload, "topiclist", &topicList, dimensions);
127
128                     for(size_t idx = 0; idx < dimensions[0]; idx++)
129                     {
130                         m_resources.push_back(std::shared_ptr<OC::OCResource>(
131                                 new OC::OCResource(m_clientWrapper, m_devAddr,
132                                                    std::string(topicList[idx]),
133                                                    "",
134                                                    OC_OBSERVABLE,
135                                                    {OC_RSRVD_RESOURCE_TYPE_MQ_TOPIC},
136                                                    {DEFAULT_INTERFACE})));
137                     }
138                 }
139             }
140
141             ListenOCContainer(std::weak_ptr<IClientWrapper> cw,
142                               OCDevAddr& devAddr, const std::string& topicUri)
143                               : m_clientWrapper(cw), m_devAddr(devAddr)
144             {
145                     m_resources.push_back(std::shared_ptr<OC::OCResource>(
146                             new OC::OCResource(m_clientWrapper, m_devAddr,
147                                                topicUri,
148                                                "",
149                                                OC_OBSERVABLE,
150                                                {OC_RSRVD_RESOURCE_TYPE_MQ_TOPIC},
151                                                {DEFAULT_INTERFACE})));
152             }
153 #endif
154
155             const std::vector<std::shared_ptr<OCResource>>& Resources() const
156             {
157                 return m_resources;
158             }
159         private:
160             std::vector<std::shared_ptr<OC::OCResource>> m_resources;
161             std::weak_ptr<IClientWrapper> m_clientWrapper;
162             OCDevAddr& m_devAddr;
163     };
164 }