Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / resource / examples / roomclient.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 // OCClient.cpp : Defines the entry point for the console application.
22 //
23 #include <string>
24 #include <cstdlib>
25 #include <pthread.h>
26 #include <mutex>
27 #include <condition_variable>
28
29 #include "OCPlatform.h"
30 #include "OCApi.h"
31
32 using namespace OC;
33
34 const int SUCCESS_RESPONSE = 0;
35 std::shared_ptr<OCResource> curResource;
36 std::mutex resourceLock;
37
38 int observe_count()
39 {
40     static int oc = 0;
41     return ++oc;
42 }
43
44 // Forward declaration
45 void putRoomRepresentation(std::shared_ptr<OCResource> resource);
46 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
47
48 void printRoomRepresentation(const OCRepresentation& rep)
49 {
50     std::cout << "\tResource URI: " << rep.getUri() << std::endl;
51
52     if(rep.hasAttribute("name"))
53     {
54         std::cout << "\tRoom name: " << rep.getValue<std::string>("name") << std::endl;
55     }
56
57     std::vector<OCRepresentation> children = rep.getChildren();
58
59     for(auto oit = children.begin(); oit != children.end(); ++oit)
60     {
61         std::cout << "\t\tChild Resource URI: " << oit->getUri() << std::endl;
62         if(oit->getUri().find("light") != std::string::npos)
63         {
64             if(oit->hasAttribute("state") && oit->hasAttribute("color"))
65             {
66                 std::cout << "\t\tstate:" << oit->getValue<bool>("state")  << std::endl;
67                 std::cout << "\t\tcolor:" << oit->getValue<int>("color")  << std::endl;
68             }
69         }
70         else if(oit->getUri().find("fan") != std::string::npos)
71         {
72             if(oit->hasAttribute("state") && oit->hasAttribute("speed"))
73             {
74                 std::cout << "\t\tstate:" << oit->getValue<bool>("state") << std::endl;
75                 std::cout << "\t\tspeed:" << oit->getValue<int>("speed") << std::endl;
76             }
77         }
78     }
79 }
80
81 // callback handler on GET request
82 void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
83 {
84     if(eCode == SUCCESS_RESPONSE)
85     {
86         std::cout << "GET request was successful" << std::endl;
87
88         printRoomRepresentation(rep);
89
90         putRoomRepresentation(curResource);
91     }
92     else
93     {
94         std::cout << "onGET Response error: " << eCode << std::endl;
95         std::exit(-1);
96     }
97 }
98
99 void onGet1(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
100 {
101     if(eCode == SUCCESS_RESPONSE)
102     {
103         std::cout << "GET request was successful" << std::endl;
104
105         printRoomRepresentation(rep);
106     }
107     else
108     {
109         std::cout << "onGET Response error: " << eCode << std::endl;
110         std::exit(-1);
111     }
112 }
113
114 // Local function to get representation of room resource
115 void getRoomRepresentation(std::shared_ptr<OCResource> resource,
116                             std::string interface, GetCallback getCallback)
117 {
118     if(resource)
119     {
120         std::cout << "Getting room representation on: "<< interface << std::endl;
121
122         resource->get("core.room", interface, QueryParamsMap(), getCallback);
123     }
124 }
125
126 // Local function to put a different state for this resource
127 void putRoomRepresentation(std::shared_ptr<OCResource> resource)
128 {
129     if(resource)
130     {
131         OCRepresentation rep;
132         std::cout << "Putting room representation on: " << BATCH_INTERFACE << std::endl;
133
134         bool state = true;
135         int speed = 10;
136         rep.setValue("state", state);
137         rep.setValue("speed", speed);
138
139         // Invoke resource's pit API with attribute map, query map and the callback parameter
140         resource->put("core.room", BATCH_INTERFACE, rep, QueryParamsMap(), &onPut);
141     }
142 }
143
144 // callback handler on PUT request
145 void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
146 {
147     if(eCode == SUCCESS_RESPONSE)
148     {
149         std::cout << "PUT request was successful" << std::endl;
150
151         printRoomRepresentation(rep);
152
153         getRoomRepresentation(curResource, DEFAULT_INTERFACE, onGet1);
154     }
155     else
156     {
157         std::cout << "onPut Response error: " << eCode << std::endl;
158         std::exit(-1);
159     }
160 }
161
162 // Callback to found resources
163 void foundResource(std::shared_ptr<OCResource> resource)
164 {
165     std::lock_guard<std::mutex> lock(resourceLock);
166     if(curResource)
167     {
168         std::cout << "Found another resource, ignoring"<<std::endl;
169         return;
170     }
171
172     std::string resourceURI;
173     std::string hostAddress;
174     try
175     {
176         // Do some operations with resource object.
177         if(resource)
178         {
179             std::cout<<"DISCOVERED Resource:"<<std::endl;
180             // Get the resource URI
181             resourceURI = resource->uri();
182             std::cout << "\tURI of the resource: " << resourceURI << std::endl;
183
184             // Get the resource host address
185             hostAddress = resource->host();
186             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
187
188             // Get the resource types
189             std::cout << "\tList of resource types: " << std::endl;
190             for(auto &resourceTypes : resource->getResourceTypes())
191             {
192                 std::cout << "\t\t" << resourceTypes << std::endl;
193             }
194
195             // Get the resource interfaces
196             std::cout << "\tList of resource interfaces: " << std::endl;
197             for(auto &resourceInterfaces : resource->getResourceInterfaces())
198             {
199                 std::cout << "\t\t" << resourceInterfaces << std::endl;
200             }
201
202             if(resourceURI == "/a/room")
203             {
204                 curResource = resource;
205                 // Call a local function which will internally invoke get API on the resource pointer
206                 getRoomRepresentation(resource, BATCH_INTERFACE, onGet);
207             }
208         }
209         else
210         {
211             // Resource is invalid
212             std::cout << "Resource is invalid" << std::endl;
213         }
214
215     }
216     catch(std::exception& e)
217     {
218         std::cerr << "Exception in foundResource: "<< e.what() <<std::endl;
219         //log(e.what());
220     }
221 }
222
223 int main(int argc, char* argv[]) {
224
225     std::ostringstream requestURI;
226
227
228     // Create PlatformConfig object
229     PlatformConfig cfg {
230         OC::ServiceType::InProc,
231         OC::ModeType::Client,
232         "0.0.0.0",
233         0,
234         OC::QualityOfService::LowQos
235     };
236
237     OCPlatform::Configure(cfg);
238
239     try
240     {
241         // Find all resources
242         requestURI << OC_MULTICAST_DISCOVERY_URI;
243
244         OCPlatform::findResource("", requestURI.str(), OC_ALL, &foundResource);
245         std::cout<< "Finding Resource... " <<std::endl;
246
247         // A condition variable will free the mutex it is given, then do a non-
248         // intensive block until 'notify' is called on it.  In this case, since we
249         // don't ever call cv.notify, this should be a non-processor intensive version
250         // of while(true);
251         std::mutex blocker;
252         std::condition_variable cv;
253         std::unique_lock<std::mutex> lock(blocker);
254         cv.wait(lock);
255
256     }catch(OCException& e)
257     {
258         oclog() << "Exception in main: "<< e.what();
259     }
260
261     return 0;
262 }
263
264