[IoTivity Simulator] Handling resource interfaces.
[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     {
29         return "Empty payload";
30     }
31     std::ostringstream data;
32     std::ostringstream valueStr;
33
34     std::string payloadType;
35     OCStringLL *types;
36     OCStringLL *interfaces;
37     OCRepPayloadValue *values;
38
39     // Iterate the payload list
40     while (payload)
41     {
42         // Payload type
43         payloadType = getPayloadTypeString(payload->base.type);
44         if (!payloadType.empty())
45             data << "Payload type: " << payloadType << std::endl;
46
47         // URI
48         if (NULL != payload->uri && strlen(payload->uri) > 0)
49             data << "URI: " << payload->uri << std::endl;
50
51         // Types
52         types = payload->types;
53         while (types)
54         {
55             if (NULL != types->value && strlen(types->value) > 0)
56             {
57                 valueStr << types->value;
58                 if (types->next)
59                     valueStr << ", ";
60             }
61             types = types->next;
62         }
63         if (!valueStr.str().empty())
64         {
65             data << "Types: " << valueStr.str() << std::endl;
66             valueStr.str("");
67         }
68
69         // Interfaces
70         interfaces = payload->interfaces;
71         while (interfaces)
72         {
73             if (NULL != interfaces->value && strlen(interfaces->value) > 0)
74             {
75                 valueStr << interfaces->value;
76                 if (interfaces->next)
77                     valueStr << ", ";
78             }
79             interfaces = interfaces->next;
80         }
81         if (!valueStr.str().empty())
82         {
83             data << "Interfaces: " << valueStr.str() << std::endl;
84             valueStr.str("");
85         }
86
87         // Values
88         values = payload->values;
89         while (values)
90         {
91             valueStr << "\t" << values->name << ":" << rep.getValueToString(values->name) << std::endl;
92             values = values->next;
93         }
94         if (!valueStr.str().empty())
95         {
96             data << "Values:-" << std::endl;
97             data << valueStr.str();
98             valueStr.str("");
99         }
100         payload = payload->next;
101         if (payload)
102             data << "----------------" << std::endl;
103     }
104
105     return data.str();
106 }
107
108 std::string getPayloadTypeString(OCPayloadType type)
109 {
110     std::string typeStr;
111     switch (type)
112     {
113         case PAYLOAD_TYPE_INVALID:
114             typeStr = "PAYLOAD_TYPE_INVALID";
115             break;
116         case PAYLOAD_TYPE_DISCOVERY:
117             typeStr = "PAYLOAD_TYPE_DISCOVERY";
118             break;
119         case PAYLOAD_TYPE_DEVICE:
120             typeStr = "PAYLOAD_TYPE_DEVICE";
121             break;
122         case PAYLOAD_TYPE_PLATFORM:
123             typeStr = "PAYLOAD_TYPE_PLATFORM";
124             break;
125         case PAYLOAD_TYPE_REPRESENTATION:
126             typeStr = "PAYLOAD_TYPE_REPRESENTATION";
127             break;
128         case PAYLOAD_TYPE_SECURITY:
129             typeStr = "PAYLOAD_TYPE_SECURITY";
130             break;
131         case PAYLOAD_TYPE_PRESENCE:
132             typeStr = "PAYLOAD_TYPE_PRESENCE";
133             break;
134     }
135     return typeStr;
136 }
137
138 std::string getRequestString(const std::map<std::string, std::string> &queryParams,
139                              const OC::OCRepresentation &rep)
140 {
141     std::ostringstream data;
142     std::ostringstream queryParamsStr;
143     if (queryParams.size() > 0)
144     {
145         for (auto &qp : queryParams)
146             queryParamsStr << qp.first << "=" << qp.second << ";";
147     }
148     if (!queryParamsStr.str().empty())
149     {
150         data << "qp: " << queryParamsStr.str() << std::endl;
151     }
152     data << getPayloadString(rep);
153     return data.str();
154 }
155
156 std::string getRequestString(const std::map<std::string, std::string> &queryParams)
157 {
158     std::ostringstream data;
159     std::ostringstream queryParamsStr;
160     if (queryParams.size() > 0)
161     {
162         for (auto &qp : queryParams)
163             queryParamsStr << qp.first << "=" << qp.second << ";";
164     }
165     if (!queryParamsStr.str().empty())
166     {
167         data << "qp: " << queryParamsStr.str() << std::endl;
168     }
169     data << "Payload:  No payload";
170     return data.str();
171 }