Imported Upstream version 1.0.1
[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                 OCResourcePayload* res = payload->resources;
48                 OCResourceCollectionPayload* colRes = payload->collectionResources;
49                 if (res)
50                 {
51                     while(res)
52                     {
53                         char uuidString[UUID_STRING_SIZE];
54                         if(OCConvertUuidToString(payload->sid, uuidString) != RAND_UUID_OK)
55                         {
56                             uuidString[0]= '\0';
57                         }
58
59                         if (res->secure)
60                         {
61                             m_devAddr.flags =
62                                   (OCTransportFlags)(OC_FLAG_SECURE | m_devAddr.flags);
63                         }
64
65                         if (res->port != 0)
66                         {
67                              m_devAddr.port = res->port;
68                         }
69
70                         m_resources.push_back(std::shared_ptr<OC::OCResource>(
71                                     new OC::OCResource(m_clientWrapper, m_devAddr,
72                                         std::string(res->uri),
73                                         std::string(uuidString),
74                                         (res->bitmap & OC_OBSERVABLE) == OC_OBSERVABLE,
75                                         StringLLToVector(res->types),
76                                         StringLLToVector(res->interfaces)
77                                         )));
78                         res = res->next;
79                     }
80                 }
81                 else if (colRes)
82                 {
83                     while(colRes)
84                     {
85                         // currently support for ipv4 is provided.
86                         OCDevAddr colAddr;
87                         colAddr.adapter = OC_ADAPTER_IP;
88                         colAddr.flags   = OC_IP_USE_V4;
89                         char *ptr = strtok(colRes->tags->baseURI, ":");
90                         OICStrcpy(colAddr.addr, sizeof(colAddr.addr), ptr);
91
92                         if (colRes->tags->bitmap & OC_SECURE)
93                         {
94                             colAddr.flags =
95                                   (OCTransportFlags)(OC_FLAG_SECURE | m_devAddr.flags);
96                         }
97                         if (colRes->tags->port != 0)
98                         {
99                              colAddr.port = colRes->tags->port;
100                         }
101                         else
102                         {
103                             colAddr.port = atoi(ptr+1);
104                         }
105
106                         m_resources.push_back(std::shared_ptr<OC::OCResource>(
107                                     new OC::OCResource(m_clientWrapper, colAddr,
108                                         std::string(colRes->setLinks->href),
109                                         std::string((char*)colRes->tags->di.id),
110                                         (colRes->tags->bitmap & OC_OBSERVABLE) == OC_OBSERVABLE,
111                                         StringLLToVector(colRes->setLinks->rt),
112                                         StringLLToVector(colRes->setLinks->itf)
113                                         )));
114                         colRes = colRes->next;
115                     }
116                 }
117             }
118
119             const std::vector<std::shared_ptr<OCResource>>& Resources() const
120             {
121                 return m_resources;
122             }
123         private:
124             std::vector<std::shared_ptr<OC::OCResource>> m_resources;
125             std::weak_ptr<IClientWrapper> m_clientWrapper;
126             OCDevAddr& m_devAddr;
127     };
128 }