Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / simulator / src / common / simulator_utils.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics 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 "simulator_utils.h"
22 #include "OCRepresentation.h"
23
24 std::string getPayloadString(const OC::OCRepresentation &rep)
25 {
26     OCRepPayload *payload = rep.getPayload();
27     if (!payload)
28         return "Empty payload";
29
30     std::ostringstream payLoadString;
31     while (payload)
32     {
33         // Payload type
34         std::string payloadType;
35         payloadType = getPayloadTypeString(payload->base.type);
36         payLoadString << "Payload type: " << payloadType << std::endl;
37
38         // URI
39         if (NULL != payload->uri && strlen(payload->uri) > 0)
40             payLoadString << "URI: " << payload->uri << std::endl;
41
42         // Types
43         std::ostringstream typeString;
44         OCStringLL *ocTypes = payload->types;
45         while (ocTypes)
46         {
47             if (NULL != ocTypes->value)
48             {
49                 typeString << ocTypes->value;
50                 if (ocTypes->next)
51                     typeString << ", ";
52             }
53
54             ocTypes = ocTypes->next;
55         }
56
57         if (!typeString.str().empty())
58         {
59             payLoadString << "Types: " << typeString.str() << std::endl;
60         }
61
62         // Interfaces
63         std::ostringstream interfaceString;
64         OCStringLL *ocInterfaces = payload->interfaces;
65         while (ocInterfaces)
66         {
67             if (NULL != ocInterfaces->value)
68             {
69                 interfaceString << ocInterfaces->value;
70                 if (ocInterfaces->next)
71                     interfaceString << ", ";
72             }
73
74             ocInterfaces = ocInterfaces->next;
75         }
76
77         if (!interfaceString.str().empty())
78         {
79             payLoadString << "Interfaces: " << interfaceString.str() << std::endl;
80         }
81
82         // Values
83         std::ostringstream valueString;
84         OCRepPayloadValue *ocValues = payload->values;
85         while (ocValues)
86         {
87             valueString << "\t" << ocValues->name << ":" << rep.getValueToString(ocValues->name) <<
88                         std::endl;
89             ocValues = ocValues->next;
90         }
91
92         if (!valueString.str().empty())
93         {
94             payLoadString << "Values:" << std::endl;
95             payLoadString << valueString.str();
96         }
97
98         payload = payload->next;
99         if (payload)
100             payLoadString << "----------------" << std::endl;
101     }
102
103     return payLoadString.str();
104 }
105
106 std::string getPayloadTypeString(OCPayloadType type)
107 {
108     std::string typeStr;
109     switch (type)
110     {
111         case PAYLOAD_TYPE_INVALID:
112             typeStr = "PAYLOAD_TYPE_INVALID";
113             break;
114         case PAYLOAD_TYPE_DISCOVERY:
115             typeStr = "PAYLOAD_TYPE_DISCOVERY";
116             break;
117         case PAYLOAD_TYPE_DEVICE:
118             typeStr = "PAYLOAD_TYPE_DEVICE";
119             break;
120         case PAYLOAD_TYPE_PLATFORM:
121             typeStr = "PAYLOAD_TYPE_PLATFORM";
122             break;
123         case PAYLOAD_TYPE_REPRESENTATION:
124             typeStr = "PAYLOAD_TYPE_REPRESENTATION";
125             break;
126         case PAYLOAD_TYPE_SECURITY:
127             typeStr = "PAYLOAD_TYPE_SECURITY";
128             break;
129         case PAYLOAD_TYPE_PRESENCE:
130             typeStr = "PAYLOAD_TYPE_PRESENCE";
131             break;
132         case PAYLOAD_TYPE_RD:
133             typeStr = "PAYLOAD_TYPE_RD";
134             break;
135     }
136     return typeStr;
137 }
138
139 std::string getRequestString(const std::map<std::string, std::string> &queryParams,
140                              const OC::OCRepresentation &rep)
141 {
142     std::ostringstream requestString;
143     if (queryParams.size() > 0)
144     {
145         requestString << "qp: ";
146         for (auto &qp : queryParams)
147             requestString << qp.first << "=" << qp.second << ";";
148     }
149
150     requestString << std::endl;
151     requestString << getPayloadString(rep);
152     return requestString.str();
153 }
154
155 std::string getRequestString(const std::map<std::string, std::string> &queryParams)
156 {
157     std::ostringstream requestString;
158     if (queryParams.size() > 0)
159     {
160         requestString << "qp: ";
161         for (auto &qp : queryParams)
162             requestString << qp.first << "=" << qp.second << ";";
163     }
164
165     requestString << std::endl;
166     return requestString.str();
167 }