replace : iotivity -> iotivity-sec
[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             {
46                 OCDevAddr currentDevAddr = devAddr;
47
48                 while (payload)
49                 {
50                     std::string deviceName;
51                     if (payload->name)
52                     {
53                         deviceName = payload->name;
54                     }
55
56                     OCResourcePayload* res = payload->resources;
57                     while (res)
58                     {
59
60                         currentDevAddr.flags = res->secure ?
61                                 (OCTransportFlags)(OC_FLAG_SECURE | devAddr.flags) :
62                                 devAddr.flags;
63
64                         currentDevAddr.port = (res->port != 0) ? res->port : devAddr.port;
65
66                         if (payload->baseURI)
67                         {
68                             OCDevAddr rdPubAddr = currentDevAddr;
69
70                             std::string baseURI = std::string(payload->baseURI);
71                             size_t len = baseURI.length();
72                             int addressLen = baseURI.find_first_of(":");
73                             std::string ipaddress = baseURI.substr(0, addressLen);
74                             int port = atoi(baseURI.substr(addressLen + 1, len).c_str());
75                             OICStrcpy(rdPubAddr.addr, addressLen + 1, ipaddress.c_str());
76                             rdPubAddr.port = port;
77                             m_resources.push_back(std::shared_ptr<OC::OCResource>(
78                                         new OC::OCResource(cw, rdPubAddr,
79                                             std::string(res->uri),
80                                             std::string(payload->sid),
81                                             res->bitmap,
82                                             StringLLToVector(res->types),
83                                             StringLLToVector(res->interfaces),
84                                             deviceName
85                                             )));
86                         }
87                         else
88                         {
89                             m_resources.push_back(std::shared_ptr<OC::OCResource>(
90                                     new OC::OCResource(cw, currentDevAddr,
91                                         std::string(res->uri),
92                                         std::string(payload->sid),
93                                         res->bitmap,
94                                         StringLLToVector(res->types),
95                                         StringLLToVector(res->interfaces),
96                                         deviceName
97                                         )));
98
99 #ifdef TCP_ADAPTER
100                             if (res->tcpPort != 0)
101                             {
102                                 OCDevAddr tcpDevAddr = currentDevAddr;
103                                 tcpDevAddr.port = res->tcpPort;
104                                 tcpDevAddr.adapter = OC_ADAPTER_TCP;
105                                 m_resources.push_back(std::shared_ptr<OC::OCResource>(
106                                             new OC::OCResource(cw, tcpDevAddr,
107                                                 std::string(res->uri),
108                                                 std::string(payload->sid),
109                                                 res->bitmap,
110                                                 StringLLToVector(res->types),
111                                                 StringLLToVector(res->interfaces),
112                                                 deviceName
113                                                 )));
114                             }
115 #endif
116                         }
117                         res = res->next;
118                     }
119                     payload = payload->next;
120                 }
121             }
122
123 #ifdef WITH_MQ
124             ListenOCContainer(std::weak_ptr<IClientWrapper> cw,
125                                 OCDevAddr& devAddr, OCRepPayload* payload)
126             {
127                 if (payload)
128                 {
129                     char**topicList = nullptr;
130                     size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0};
131                     OCRepPayloadGetStringArray(payload, "topiclist", &topicList, dimensions);
132
133                     for(size_t idx = 0; idx < dimensions[0]; idx++)
134                     {
135                         m_resources.push_back(std::shared_ptr<OC::OCResource>(
136                                 new OC::OCResource(cw, devAddr,
137                                                    std::string(topicList[idx]),
138                                                    "",
139                                                    OC_OBSERVABLE,
140                                                    {OC_RSRVD_RESOURCE_TYPE_MQ_TOPIC},
141                                                    {DEFAULT_INTERFACE},
142                                                    deviceName
143                                                     )));
144                     }
145                 }
146             }
147
148             ListenOCContainer(std::weak_ptr<IClientWrapper> cw,
149                               OCDevAddr& devAddr, const std::string& topicUri)
150             {
151                     m_resources.push_back(std::shared_ptr<OC::OCResource>(
152                             new OC::OCResource(cw, devAddr,
153                                                topicUri,
154                                                "",
155                                                OC_OBSERVABLE,
156                                                {OC_RSRVD_RESOURCE_TYPE_MQ_TOPIC},
157                                                {DEFAULT_INTERFACE},
158                                                deviceName
159                                                 )));
160             }
161 #endif
162
163             const std::vector<std::shared_ptr<OCResource>>& Resources() const
164             {
165                 return m_resources;
166             }
167         private:
168             std::vector<std::shared_ptr<OC::OCResource>> m_resources;
169     };
170 }