Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / resource / 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 <mutex>
27 #include <condition_variable>
28 #include "OCPlatform.h"
29 #include "OCApi.h"
30
31 using namespace OC;
32
33 const int SUCCESS_RESPONSE = 0;
34 std::shared_ptr<OCResource> curResource;
35 std::mutex curResourceLock;
36
37 static void printUsage()
38 {
39     std::cout<<"Usage: garageclient <0|1> \n";
40     std::cout<<"ConnectivityType: Default IP\n";
41     std::cout<<"ConnectivityType 0: IP \n";
42 }
43
44 class Garage
45 {
46 public:
47
48     bool m_state;
49     std::string m_name;
50     std::vector<bool> m_lightStates;
51     std::vector<int> m_lightPowers;
52     OCRepresentation m_lightRep;
53     std::vector<OCRepresentation> m_reps;
54     std::vector<std::vector<int>> m_hingeStates;
55
56     Garage() : m_state(false), m_name("")
57     {
58     }
59 };
60
61 Garage myGarage;
62
63 void printRepresentation(const OCRepresentation& rep)
64 {
65
66         // Check if attribute "name" exists, and then getValue
67         if(rep.hasAttribute("name"))
68         {
69             myGarage.m_name = rep["name"];
70         }
71         std::cout << "\tname: " << myGarage.m_name << std::endl;
72
73         // You can directly try to get the value. this function
74         // return false if there is no attribute "state"
75         if(!rep.getValue("state", myGarage.m_state))
76         {
77             std::cout << "Attribute state doesn't exist in the representation\n";
78         }
79         std::cout << "\tstate: " << myGarage.m_state << std::endl;
80
81         OCRepresentation rep2 = rep;
82
83         std::cout << "Number of attributes in rep2: "
84                   << rep2.numberOfAttributes() << std::endl;
85
86         if(rep2.erase("name"))
87         {
88             std::cout << "attribute: name, was removed successfully from rep2.\n";
89         }
90
91         std::cout << "Number of attributes in rep2: "
92                   << rep2.numberOfAttributes() << std::endl;
93
94
95         if(rep.isNULL("nullAttribute"))
96         {
97             std::cout << "\tnullAttribute is null." << std::endl;
98         }
99         else
100         {
101             std::cout << "\tnullAttribute is not null." << std::endl;
102         }
103
104         myGarage.m_lightRep = rep["light"];
105
106         myGarage.m_lightStates = myGarage.m_lightRep["states"];
107         myGarage.m_lightPowers = myGarage.m_lightRep["powers"];
108
109         std::cout << "\tlightRep: states: ";
110
111         int first = 1;
112         for(auto state: myGarage.m_lightStates)
113         {
114             if(first)
115             {
116                 std::cout << state;
117                 first = 0;
118             }
119             else
120             {
121                 std::cout << "," << state;
122             }
123         }
124
125         std::cout << std::endl;
126         std::cout << "\tlightRep: powers: ";
127         first = 1;
128         for(auto power: myGarage.m_lightPowers)
129         {
130             if(first)
131             {
132                 std::cout << power;
133                 first = 0;
134             }
135             else
136             {
137                 std::cout << "," << power;
138             }
139         }
140         std::cout << std::endl;
141
142         // Get vector of representations
143         myGarage.m_reps = rep["reps"];
144
145         int ct = 0;
146         for(auto& rep : myGarage.m_reps)
147         {
148             for(auto& attribute : rep)
149             {
150                 std::cout<< "\treps["<<ct<<"]."<<attribute.attrname()<<":"
151                     << attribute.type()<<" with value " <<attribute.getValueToString() <<std::endl;
152             }
153             ++ct;
154         }
155
156         std::cout << "\tjson: " << rep["json"] << std::endl;
157         myGarage.m_hingeStates = rep["hinges"];
158
159         std::cout<< "\tHinge parameter is type: " << rep["hinges"].type() << " with depth "<<
160             rep["hinges"].depth() << " and a base type of "<< rep["hinges"].base_type()<<std::endl;
161
162
163 }
164 // callback handler on PUT request
165 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
166 {
167     if(eCode == SUCCESS_RESPONSE)
168     {
169         std::cout << "PUT request was successful" << std::endl;
170
171         printRepresentation(rep);
172     }
173     else
174     {
175         std::cout << "onPut Response error: " << eCode << std::endl;
176         std::exit(-1);
177     }
178 }
179
180 // Local function to put a different state for this resource
181 void putLightRepresentation(std::shared_ptr<OCResource> resource)
182 {
183     if(resource)
184     {
185         OCRepresentation rep;
186
187         std::cout << "Putting light representation..."<<std::endl;
188
189         myGarage.m_state = true;
190
191         rep["state"] = myGarage.m_state;
192
193         // Create QueryParameters Map and add query params (if any)
194         QueryParamsMap queryParamsMap;
195
196         // Invoke resource's pit API with rep, query map and the callback parameter
197         resource->put(rep, queryParamsMap, &onPut);
198     }
199 }
200
201 // Callback handler on GET request
202 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
203 {
204     if(eCode == SUCCESS_RESPONSE)
205     {
206         std::cout << "GET request was successful" << std::endl;
207         std::cout << "Resource URI: " << rep.getUri() << std::endl;
208
209         printRepresentation(rep);
210
211         putLightRepresentation(curResource);
212     }
213     else
214     {
215         std::cout << "onGET Response error: " << eCode << std::endl;
216         std::exit(-1);
217     }
218 }
219
220 // Local function to get representation of light resource
221 void getLightRepresentation(std::shared_ptr<OCResource> resource)
222 {
223     if(resource)
224     {
225         std::cout << "Getting Light Representation..."<<std::endl;
226         // Invoke resource's get API with the callback parameter
227
228         QueryParamsMap test;
229         resource->get(test, &onGet);
230     }
231 }
232
233 // Callback to found resources
234 void foundResource(std::shared_ptr<OCResource> resource)
235 {
236     std::lock_guard<std::mutex> lock(curResourceLock);
237     if(curResource)
238     {
239         std::cout << "Found another resource, ignoring"<<std::endl;
240         return;
241     }
242
243     std::string resourceURI;
244     std::string hostAddress;
245     try
246     {
247         // Do some operations with resource object.
248         if(resource)
249         {
250             std::cout<<"DISCOVERED Resource:"<<std::endl;
251             // Get the resource URI
252             resourceURI = resource->uri();
253             std::cout << "\tURI of the resource: " << resourceURI << std::endl;
254
255             // Get the resource host address
256             hostAddress = resource->host();
257             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
258
259             // Get the resource types
260             std::cout << "\tList of resource types: " << std::endl;
261             for(auto &resourceTypes : resource->getResourceTypes())
262             {
263                 std::cout << "\t\t" << resourceTypes << std::endl;
264             }
265
266             // Get the resource interfaces
267             std::cout << "\tList of resource interfaces: " << std::endl;
268             for(auto &resourceInterfaces : resource->getResourceInterfaces())
269             {
270                 std::cout << "\t\t" << resourceInterfaces << std::endl;
271             }
272
273             if(resourceURI == "/a/garage")
274             {
275                 curResource = resource;
276                 // Call a local function which will internally invoke
277                 // get API on the resource pointer
278                 getLightRepresentation(resource);
279             }
280         }
281         else
282         {
283             // Resource is invalid
284             std::cout << "Resource is invalid" << std::endl;
285         }
286
287     }
288     catch(std::exception& e)
289     {
290         std::cerr << "Exception in foundResource: "<< e.what()<<std::endl;
291     }
292 }
293
294 int main(int argc, char* argv[]) {
295
296     std::ostringstream requestURI;
297
298     OCConnectivityType connectivityType = CT_ADAPTER_IP;
299
300     if(argc == 2)
301     {
302         try
303         {
304             std::size_t inputValLen;
305             int optionSelected = std::stoi(argv[1], &inputValLen);
306
307             if(inputValLen == strlen(argv[1]))
308             {
309                 if(optionSelected == 0)
310                 {
311                     std::cout << "Using IP."<< std::endl;
312                     connectivityType = CT_ADAPTER_IP;
313                 }
314                 else
315                 {
316                     std::cout << "Invalid connectivity type selected. Using default IP"
317                         << std::endl;
318                 }
319             }
320             else
321             {
322                 std::cout << "Invalid connectivity type selected. Using default IP" << std::endl;
323             }
324         }
325         catch(std::exception& e)
326         {
327             std::cout << "Invalid input argument. Using IP as connectivity type" << std::endl;
328         }
329     }
330     else
331     {
332         printUsage();
333         std::cout << "Invalid input argument. Using IP as connectivity type" << std::endl;
334     }
335
336     // Create PlatformConfig object
337     PlatformConfig cfg {
338         OC::ServiceType::InProc,
339         OC::ModeType::Client,
340         "0.0.0.0",
341         0,
342         OC::QualityOfService::LowQos
343     };
344
345     OCPlatform::Configure(cfg);
346     try
347     {
348         // Find all resources
349         requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.garage";
350
351         OCPlatform::findResource("", requestURI.str(),
352                 connectivityType, &foundResource);
353
354         std::cout<< "Finding Resource... " <<std::endl;
355
356         // A condition variable will free the mutex it is given, then do a non-
357         // intensive block until 'notify' is called on it.  In this case, since we
358         // don't ever call cv.notify, this should be a non-processor intensive version
359         // of while(true);
360         std::mutex blocker;
361         std::condition_variable cv;
362         std::unique_lock<std::mutex> lock(blocker);
363         cv.wait(lock);
364     }
365     catch(OCException& e)
366     {
367         std::cerr << "Exception in GarageClient: "<<e.what();
368     }
369
370     return 0;
371 }
372
373