Get/Put overload implementation in OCResource. Allows for specifiying interface...
[platform/upstream/iotivity.git] / 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 "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 int observe_count()
35 {
36     static int oc = 0;
37     return ++oc;
38 }
39
40 // Forward declaration
41 void putRoomRepresentation(std::shared_ptr<OCResource> resource);
42 void onPut(const OCRepresentation& rep, const int eCode);
43
44 // callback handler on GET request
45 void onGet(const OCRepresentation& rep, const int eCode)
46 {
47     if(eCode == SUCCESS_RESPONSE)
48     {
49         std::cout << "GET request was successful" << std::endl;
50
51         AttributeMap attributeMap = rep.getAttributeMap();
52         
53         std::cout << "Resource URI: " << rep.getUri() << std::endl;
54
55         for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
56         {
57             std::cout << "\tAttribute name: "<< it->first << " value: ";
58             for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
59             {
60                 std::cout <<"\t"<< *valueItr << " ";
61             }
62
63             std::cout << std::endl;
64         }
65
66         std::vector<OCRepresentation> children = rep.getChildren();
67
68         for(auto oit = children.begin(); oit != children.end(); ++oit)
69         {
70             std::cout << "Child Resource URI: " << oit->getUri() << std::endl;
71
72             attributeMap = oit->getAttributeMap();
73
74             for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
75             {
76                 std::cout << "\tAttribute name: "<< it->first << " value: ";
77                 for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
78                 {
79                     std::cout <<"\t"<< *valueItr << " ";
80                 }
81
82                 std::cout << std::endl;
83             }
84         }
85
86         putRoomRepresentation(curResource);
87     }
88     else
89     {
90         std::cout << "onGET Response error: " << eCode << std::endl;
91         std::exit(-1);
92     }
93 }
94
95 // Local function to put a different state for this resource
96 void putRoomRepresentation(std::shared_ptr<OCResource> resource)
97 {
98     if(resource)
99     {
100         OCRepresentation rep;
101         std::cout << "Putting room representation..."<<std::endl;
102         // Create AttributeMap
103         AttributeMap attributeMap;
104         // Add the attribute name and values in the attribute map
105         AttributeValues stateVal;
106         stateVal.push_back("true");
107
108         AttributeValues powerVal;
109         powerVal.push_back("8");
110
111         attributeMap["state"] = stateVal;
112         attributeMap["speed"] = powerVal;
113
114         // Create QueryParameters Map and add query params (if any)
115         QueryParamsMap qp;
116         rep.setAttributeMap(attributeMap);
117
118         // Invoke resource's pit API with attribute map, query map and the callback parameter
119         resource->put("core.room", BATCH_INTERFACE, rep, qp, &onPut);
120     }
121 }
122
123 // callback handler on PUT request
124 void onPut(const OCRepresentation& rep, const int eCode)
125 {
126     if(eCode == SUCCESS_RESPONSE)
127     {
128         std::cout << "PUT request was successful" << std::endl;
129
130         AttributeMap attributeMap = rep.getAttributeMap();
131
132         for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
133         {
134             std::cout << "\tAttribute name: "<< it->first << " value: ";
135             for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
136             {
137                 std::cout <<"\t"<< *valueItr << " ";
138             }
139
140             std::cout << std::endl;
141         }
142
143         std::vector<OCRepresentation> children = rep.getChildren();
144
145         for(auto oit = children.begin(); oit != children.end(); ++oit)
146         {
147             attributeMap = oit->getAttributeMap();
148
149             for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
150             {
151                 std::cout << "\tAttribute name: "<< it->first << " value: ";
152                 for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
153                 {
154                     std::cout <<"\t"<< *valueItr << " ";
155                 }
156
157                 std::cout << std::endl;
158             }
159         }
160
161     }
162     else
163     {
164         std::cout << "onPut Response error: " << eCode << std::endl;
165         std::exit(-1);
166     }
167 }
168
169
170 // Local function to get representation of light resource
171 void getRoomRepresentation(std::shared_ptr<OCResource> resource)
172 {
173     if(resource)
174     {
175         std::cout << "Getting Room Representation..."<<std::endl;
176         // Invoke resource's get API with the callback parameter
177
178         QueryParamsMap qp;
179         resource->get("core.room", BATCH_INTERFACE, qp, &onGet);
180     }
181 }
182
183 // Callback to found resources
184 void foundResource(std::shared_ptr<OCResource> resource)
185 {
186     if(curResource)
187     {
188         std::cout << "Found another resource, ignoring"<<std::endl;
189     }
190
191     std::string resourceURI;
192     std::string hostAddress;
193     try
194     {
195         // Do some operations with resource object.
196         if(resource)
197         {
198             std::cout<<"DISCOVERED Resource:"<<std::endl;
199             // Get the resource URI
200             resourceURI = resource->uri();
201             std::cout << "\tURI of the resource: " << resourceURI << std::endl;
202
203             // Get the resource host address
204             hostAddress = resource->host();
205             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
206
207             // Get the resource types 
208             std::cout << "\tList of resource types: " << std::endl;
209             for(auto &resourceTypes : resource->getResourceTypes())
210             {
211                 std::cout << "\t\t" << resourceTypes << std::endl;
212             }
213             
214             // Get the resource interfaces
215             std::cout << "\tList of resource interfaces: " << std::endl;
216             for(auto &resourceInterfaces : resource->getResourceInterfaces())
217             {
218                 std::cout << "\t\t" << resourceInterfaces << std::endl;
219             } 
220
221             if(resourceURI == "/a/room")
222             {
223                 curResource = resource;
224                 // Call a local function which will internally invoke get API on the resource pointer
225                 getRoomRepresentation(resource);
226             }
227         }
228         else
229         {
230             // Resource is invalid
231             std::cout << "Resource is invalid" << std::endl;
232         }
233
234     }
235     catch(std::exception& e)
236     {
237         //log(e.what());
238     }
239 }
240
241 int main(int argc, char* argv[]) {
242
243     // Create PlatformConfig object
244     PlatformConfig cfg {
245         OC::ServiceType::InProc,
246         OC::ModeType::Client,
247         "192.168.1.10",
248         5683,
249         OC::QualityOfService::NonConfirmable
250     };
251
252     // Create a OCPlatform instance.
253     // Note: Platform creation is synchronous call.
254
255     try
256     {
257         OCPlatform platform(cfg);
258         std::cout << "Created Platform..."<<std::endl;
259         // Find all resources
260         platform.findResource("", "coap://224.0.1.187/oc/core", &foundResource);
261         std::cout<< "Finding Resource... " <<std::endl;
262         while(true)
263         {
264             // some operations
265         }
266
267     }catch(OCException& e)
268     {
269         //log(e.what());
270     }
271
272     return 0;
273 }
274