6ce3591dc55f791d0fb6bc915d9963166a79a1fb
[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*/,
89         const OCRepresentation& rep, const int eCode)
90 {
91     if(eCode == SUCCESS_RESPONSE)
92     {
93         std::cout << "GET request was successful" << std::endl;
94
95         printRoomRepresentation(rep);
96
97         putRoomRepresentation(curResource);
98     }
99     else
100     {
101         std::cout << "onGET Response error: " << eCode << std::endl;
102         std::exit(-1);
103     }
104 }
105
106 void onGet1(const HeaderOptions& /*headerOptions*/,
107         const OCRepresentation& rep, const int eCode)
108 {
109     if(eCode == SUCCESS_RESPONSE)
110     {
111         std::cout << "GET request was successful" << std::endl;
112
113         printRoomRepresentation(rep);
114     }
115     else
116     {
117         std::cout << "onGET Response error: " << eCode << std::endl;
118         std::exit(-1);
119     }
120 }
121
122 // Local function to get representation of room resource
123 void getRoomRepresentation(std::shared_ptr<OCResource> resource,
124                             std::string interface, GetCallback getCallback)
125 {
126     if(resource)
127     {
128         std::cout << "Getting room representation on: "<< interface << std::endl;
129
130         resource->get("core.room", interface, QueryParamsMap(), getCallback);
131     }
132 }
133
134 // Local function to put a different state for this resource
135 void putRoomRepresentation(std::shared_ptr<OCResource> resource)
136 {
137     if(resource)
138     {
139         OCRepresentation rep;
140         std::cout << "Putting room representation on: " << BATCH_INTERFACE << std::endl;
141
142         bool state = true;
143         int speed = 10;
144         rep.setValue("state", state);
145         rep.setValue("speed", speed);
146
147         // Invoke resource's pit API with attribute map, query map and the callback parameter
148         resource->put("core.room", BATCH_INTERFACE, rep, QueryParamsMap(), &onPut);
149     }
150 }
151
152 // callback handler on PUT request
153 void onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
154 {
155     if(eCode == SUCCESS_RESPONSE)
156     {
157         std::cout << "PUT request was successful" << std::endl;
158
159         printRoomRepresentation(rep);
160
161         getRoomRepresentation(curResource, DEFAULT_INTERFACE, onGet1);
162     }
163     else
164     {
165         std::cout << "onPut Response error: " << eCode << std::endl;
166         std::exit(-1);
167     }
168 }
169
170 // Callback to found resources
171 void foundResource(std::shared_ptr<OCResource> resource)
172 {
173     std::lock_guard<std::mutex> lock(resourceLock);
174     if(curResource)
175     {
176         std::cout << "Found another resource, ignoring"<<std::endl;
177         return;
178     }
179
180     std::string resourceURI;
181     std::string hostAddress;
182     std::string platformDiscoveryURI = "/oic/p";
183     std::string deviceDiscoveryURI   = "/oic/d";
184     try
185     {
186         // Do some operations with resource object.
187         if(resource)
188         {
189             std::cout<<"DISCOVERED Resource:"<<std::endl;
190             // Get the resource URI
191             resourceURI = resource->uri();
192             std::cout << "\tURI of the resource: " << resourceURI << std::endl;
193
194             // Get the resource host address
195             hostAddress = resource->host();
196             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
197
198             // Get the resource types
199             std::cout << "\tList of resource types: " << std::endl;
200             for(auto &resourceTypes : resource->getResourceTypes())
201             {
202                 std::cout << "\t\t" << resourceTypes << std::endl;
203             }
204
205             // Get the resource interfaces
206             std::cout << "\tList of resource interfaces: " << std::endl;
207             for(auto &resourceInterfaces : resource->getResourceInterfaces())
208             {
209                 std::cout << "\t\t" << resourceInterfaces << std::endl;
210             }
211
212             OCStackResult ret;
213             std::cout << "Querying for platform information... " << std::endl;
214
215             ret = OCPlatform::getPlatformInfo("", platformDiscoveryURI, CT_ADAPTER_IP, NULL);
216
217             if (ret == OC_STACK_OK)
218             {
219                 std::cout << "Get platform information is done." << std::endl;
220             }
221             else
222             {
223                 std::cout << "Get platform information failed." << std::endl;
224             }
225
226             std::cout << "Querying for device information... " << std::endl;
227
228             ret = OCPlatform::getDeviceInfo(resource->host(), deviceDiscoveryURI, CT_ADAPTER_IP,
229                     NULL);
230
231             if (ret == OC_STACK_OK)
232             {
233                 std::cout << "Getting device information is done." << std::endl;
234             }
235             else
236             {
237                 std::cout << "Getting device information failed." << std::endl;
238             }
239
240             if(resourceURI == "/a/room")
241             {
242                 curResource = resource;
243                 // Call a local function which will internally invoke get API on the resource pointer
244                 getRoomRepresentation(resource, BATCH_INTERFACE, onGet);
245             }
246         }
247         else
248         {
249             // Resource is invalid
250             std::cout << "Resource is invalid" << std::endl;
251         }
252
253     }
254     catch(std::exception& e)
255     {
256         std::cerr << "Exception caught in Found Resource: "<< e.what() <<std::endl;
257     }
258 }
259
260 int main(int argc, char* argv[]) {
261
262     std::ostringstream requestURI;
263
264     OCConnectivityType connectivityType = CT_ADAPTER_IP;
265     if(argc == 2)
266     {
267         try
268         {
269             std::size_t inputValLen;
270             int optionSelected = std::stoi(argv[1], &inputValLen);
271
272             if(inputValLen == strlen(argv[1]))
273             {
274                 if(optionSelected == 0)
275                 {
276                     std::cout << "Using IP."<< std::endl;
277                     connectivityType = CT_ADAPTER_IP;
278                 }
279                 else
280                 {
281                     std::cout << "Invalid connectivity type selected. Using default IP"<< std::endl;
282                 }
283             }
284             else
285             {
286                 std::cout << "Invalid connectivity type selected. Using default IP" << std::endl;
287             }
288         }
289         catch(std::exception&)
290         {
291             std::cout << "Invalid input argument. Using IP as connectivity type" << std::endl;
292         }
293     }
294     else
295     {
296         std::cout << "Default input argument. Using IP as Default connectivity type" << std::endl;
297         printUsage();
298     }
299
300     // Create PlatformConfig object
301     PlatformConfig cfg {
302         OC::ServiceType::InProc,
303         OC::ModeType::Client,
304         "0.0.0.0",
305         0,
306         OC::QualityOfService::LowQos
307     };
308
309     OCPlatform::Configure(cfg);
310
311     try
312     {
313         // Find all resources
314         requestURI << OC_RSRVD_WELL_KNOWN_URI;
315
316         OCPlatform::findResource("", requestURI.str(), connectivityType, &foundResource);
317         std::cout<< "Finding Resource... " <<std::endl;
318
319         // A condition variable will free the mutex it is given, then do a non-
320         // intensive block until 'notify' is called on it.  In this case, since we
321         // don't ever call cv.notify, this should be a non-processor intensive version
322         // of while(true);
323         std::mutex blocker;
324         std::condition_variable cv;
325         std::unique_lock<std::mutex> lock(blocker);
326         cv.wait(lock);
327
328     }catch(OCException& e)
329     {
330         oclog() << "Exception in main: "<< e.what();
331     }
332
333     return 0;
334 }
335
336