693f546ad926760a63aa9a75f5fc3944d66ad805
[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                             OICStrcpy(rdPubAddr.addr, sizeof(rdPubAddr.addr), payload->baseURI);
67                             rdPubAddr.port = res->port;
68                             m_resources.push_back(std::shared_ptr<OC::OCResource>(
69                                         new OC::OCResource(m_clientWrapper, rdPubAddr,
70                                             std::string(res->uri),
71                                             std::string(payload->sid),
72                                             res->bitmap,
73                                             StringLLToVector(res->types),
74                                             StringLLToVector(res->interfaces)
75                                             )));
76                         }
77                         else
78                         {
79                             m_resources.push_back(std::shared_ptr<OC::OCResource>(
80                                     new OC::OCResource(m_clientWrapper, m_devAddr,
81                                         std::string(res->uri),
82                                         std::string(payload->sid),
83                                         res->bitmap,
84                                         StringLLToVector(res->types),
85                                         StringLLToVector(res->interfaces)
86                                         )));
87
88 #ifdef TCP_ADAPTER
89                             if (res->tcpPort != 0)
90                             {
91                                 OCDevAddr tcpDevAddr = m_devAddr;
92                                 tcpDevAddr.port = res->tcpPort;
93                                 tcpDevAddr.adapter = OC_ADAPTER_TCP;
94                                 m_resources.push_back(std::shared_ptr<OC::OCResource>(
95                                             new OC::OCResource(m_clientWrapper, tcpDevAddr,
96                                                 std::string(res->uri),
97                                                 std::string(payload->sid),
98                                                 res->bitmap,
99                                                 StringLLToVector(res->types),
100                                                 StringLLToVector(res->interfaces)
101                                                 )));
102                             }
103 #endif
104                         }
105                         res = res->next;
106                     }
107                     payload = payload->next;
108                 }
109             }
110
111 #ifdef WITH_MQ
112             ListenOCContainer(std::weak_ptr<IClientWrapper> cw,
113                                 OCDevAddr& devAddr, OCRepPayload* payload)
114                                 : m_clientWrapper(cw), m_devAddr(devAddr)
115             {
116                 if (payload)
117                 {
118                     char**topicList = nullptr;
119                     size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0};
120                     OCRepPayloadGetStringArray(payload, "topiclist", &topicList, dimensions);
121
122                     for(size_t idx = 0; idx < dimensions[0]; idx++)
123                     {
124                         m_resources.push_back(std::shared_ptr<OC::OCResource>(
125                                 new OC::OCResource(m_clientWrapper, m_devAddr,
126                                                    std::string(topicList[idx]),
127                                                    "",
128                                                    OC_OBSERVABLE,
129                                                    {OC_RSRVD_RESOURCE_TYPE_MQ_TOPIC},
130                                                    {DEFAULT_INTERFACE})));
131                     }
132                 }
133             }
134
135             ListenOCContainer(std::weak_ptr<IClientWrapper> cw,
136                               OCDevAddr& devAddr, const std::string& topicUri)
137                               : m_clientWrapper(cw), m_devAddr(devAddr)
138             {
139                     m_resources.push_back(std::shared_ptr<OC::OCResource>(
140                             new OC::OCResource(m_clientWrapper, m_devAddr,
141                                                topicUri,
142                                                "",
143                                                OC_OBSERVABLE,
144                                                {OC_RSRVD_RESOURCE_TYPE_MQ_TOPIC},
145                                                {DEFAULT_INTERFACE})));
146             }
147 #endif
148
149             const std::vector<std::shared_ptr<OCResource>>& Resources() const
150             {
151                 return m_resources;
152             }
153         private:
154             std::vector<std::shared_ptr<OC::OCResource>> m_resources;
155             std::weak_ptr<IClientWrapper> m_clientWrapper;
156             OCDevAddr& m_devAddr;
157     };
158 }