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