e1b4e5cd64947898839af4739ae4fa46b1088faf
[platform/upstream/iotivity.git] / examples / garageclient.cpp
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 // garageclient.cpp is the client program for garageserver.cpp, which
22 // uses different representation in OCRepresention.
23
24 #include <string>
25 #include <cstdlib>
26 #include "OCPlatform.h"
27 #include "OCApi.h"
28
29 using namespace OC;
30
31 const int SUCCESS_RESPONSE = 0;
32 std::shared_ptr<OCResource> curResource;
33
34 class Garage
35 {
36 public:
37
38     bool m_state;
39     std::string m_name;
40     std::vector<bool> m_lightStates;
41     std::vector<int> m_lightPowers;
42     OCRepresentation m_lightRep;
43     std::vector<OCRepresentation> m_reps;
44
45     Garage() : m_state(false), m_name("")
46     {
47     }
48 };
49
50 Garage myGarage;
51
52 void printRepresentation(const OCRepresentation& rep)
53 {
54
55         // Check if attribute "name" exists, and then getValue
56         if(rep.hasAttribute("name"))
57         {
58             myGarage.m_name = rep.getValue<std::string>("name");
59         }
60         std::cout << "\tname: " << myGarage.m_name << std::endl;
61
62         // You can directly try to get the value. this function
63         // return false if there is no attribute "state"
64         if(!rep.getValue("state", myGarage.m_state))
65         {
66             std::cout << "Attribute state doesn't exist in the representation\n";
67         }
68         std::cout << "\tstate: " << myGarage.m_state << std::endl;
69
70         OCRepresentation rep2 = rep;
71
72         std::cout << "Number of attributes in rep2: "
73                   << rep2.numberOfAttributes() << std::endl;
74
75         if(rep2.erase("name"))
76         {
77             std::cout << "attribute: name, was removed successfully from rep2.\n";
78         }
79
80         std::cout << "Number of attributes in rep2: "
81                   << rep2.numberOfAttributes() << std::endl;
82
83
84         if(rep.isNULL("nullAttribute"))
85         {
86             std::cout << "\tnullAttribute is null." << std::endl;
87         }
88         else
89         {
90             std::cout << "\tnullAttribute is not null." << std::endl;
91         }
92
93         rep.getValue("light", myGarage.m_lightRep);
94
95         myGarage.m_lightRep.getValue("states", myGarage.m_lightStates);
96         myGarage.m_lightRep.getValue("powers", myGarage.m_lightPowers);
97
98         std::cout << "\tlightRep: states: ";
99
100         int first = 1;
101         for(auto state: myGarage.m_lightStates)
102         {
103             if(first)
104             {
105                 std::cout << state;
106                 first = 0;
107             }
108             else
109             {
110                 std::cout << "," << state;
111             }
112         }
113
114         std::cout << std::endl;
115         std::cout << "\tlightRep: powers: ";
116         first = 1;
117         for(auto power: myGarage.m_lightPowers)
118         {
119             if(first)
120             {
121                 std::cout << power;
122                 first = 0;
123             }
124             else
125             {
126                 std::cout << "," << power;
127             }
128         }
129         std::cout << std::endl;
130
131         // Get vector of representations
132         rep.getValue("reps", myGarage.m_reps);
133         // Client know that server is sending two representations
134         // and has key1 and key2 repsectively
135         std::cout << "\treps[0].key1: " << myGarage.m_reps[0].getValue<int>("key1") << std::endl;
136         std::cout << "\treps[0].key2: " << myGarage.m_reps[1].getValue<int>("key2") << std::endl;
137
138         std::cout << "\tjson: " << rep.getValue<std::string>("json") << std::endl;
139 }
140 // callback handler on PUT request
141 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
142 {
143     if(eCode == SUCCESS_RESPONSE)
144     {
145         std::cout << "PUT request was successful" << std::endl;
146
147         printRepresentation(rep);
148     }
149     else
150     {
151         std::cout << "onPut Response error: " << eCode << std::endl;
152         std::exit(-1);
153     }
154 }
155
156 // Local function to put a different state for this resource
157 void putLightRepresentation(std::shared_ptr<OCResource> resource)
158 {
159     if(resource)
160     {
161         OCRepresentation rep;
162
163         std::cout << "Putting light representation..."<<std::endl;
164
165         myGarage.m_state = true;
166
167         rep.setValue("state", myGarage.m_state);
168
169         // Create QueryParameters Map and add query params (if any)
170         QueryParamsMap queryParamsMap;
171
172         // Invoke resource's pit API with rep, query map and the callback parameter
173         resource->put(rep, queryParamsMap, &onPut);
174     }
175 }
176
177 // Callback handler on GET request
178 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
179 {
180     if(eCode == SUCCESS_RESPONSE)
181     {
182         std::cout << "GET request was successful" << std::endl;
183         std::cout << "Resource URI: " << rep.getUri() << std::endl;
184
185         printRepresentation(rep);
186
187         putLightRepresentation(curResource);
188     }
189     else
190     {
191         std::cout << "onGET Response error: " << eCode << std::endl;
192         std::exit(-1);
193     }
194 }
195
196 // Local function to get representation of light resource
197 void getLightRepresentation(std::shared_ptr<OCResource> resource)
198 {
199     if(resource)
200     {
201         std::cout << "Getting Light Representation..."<<std::endl;
202         // Invoke resource's get API with the callback parameter
203
204         QueryParamsMap test;
205         resource->get(test, &onGet);
206     }
207 }
208
209 // Callback to found resources
210 void foundResource(std::shared_ptr<OCResource> resource)
211 {
212     if(curResource)
213     {
214         std::cout << "Found another resource, ignoring"<<std::endl;
215     }
216
217     std::string resourceURI;
218     std::string hostAddress;
219     try
220     {
221         // Do some operations with resource object.
222         if(resource)
223         {
224             std::cout<<"DISCOVERED Resource:"<<std::endl;
225             // Get the resource URI
226             resourceURI = resource->uri();
227             std::cout << "\tURI of the resource: " << resourceURI << std::endl;
228
229             // Get the resource host address
230             hostAddress = resource->host();
231             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
232
233             // Get the resource types
234             std::cout << "\tList of resource types: " << std::endl;
235             for(auto &resourceTypes : resource->getResourceTypes())
236             {
237                 std::cout << "\t\t" << resourceTypes << std::endl;
238             }
239
240             // Get the resource interfaces
241             std::cout << "\tList of resource interfaces: " << std::endl;
242             for(auto &resourceInterfaces : resource->getResourceInterfaces())
243             {
244                 std::cout << "\t\t" << resourceInterfaces << std::endl;
245             }
246
247             if(resourceURI == "/a/garage")
248             {
249                 curResource = resource;
250                 // Call a local function which will internally invoke
251                 // get API on the resource pointer
252                 getLightRepresentation(resource);
253             }
254         }
255         else
256         {
257             // Resource is invalid
258             std::cout << "Resource is invalid" << std::endl;
259         }
260
261     }
262     catch(std::exception& e)
263     {
264         //log(e.what());
265     }
266 }
267
268 int main(int argc, char* argv[]) {
269
270     // Create PlatformConfig object
271     PlatformConfig cfg {
272         OC::ServiceType::InProc,
273         OC::ModeType::Client,
274         "0.0.0.0",
275         0,
276         OC::QualityOfService::LowQos
277     };
278
279     OCPlatform::Configure(cfg);
280     try
281     {
282         // Find all resources
283         OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.garage",
284                     &foundResource);
285         std::cout<< "Finding Resource... " <<std::endl;
286         while(true)
287         {
288             // some operations
289         }
290
291     }
292     catch(OCException& e)
293     {
294         std::cerr << "Exception in GarageClient: "<<e.what();
295     }
296
297     return 0;
298 }
299