New APIs : hasAttribute, numberOfAttributes, erase in OCRepresentation.
[platform/upstream/iotivity.git] / 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 <pthread.h>
27 #include "OCPlatform.h"
28 #include "OCApi.h"
29
30 using namespace OC;
31
32 const int SUCCESS_RESPONSE = 0;
33 std::shared_ptr<OCResource> curResource;
34
35 class Garage
36 {
37 public:
38
39     bool m_state;
40     std::string m_name;
41     std::vector<bool> m_lightStates;
42     std::vector<int> m_lightPowers;
43     OCRepresentation m_lightRep;
44
45     Garage() : m_state(false), m_name("")
46     {
47     }
48 };
49
50 Garage myGarage;
51
52 void printRepresentation(const OCRepresentation& rep)
53 {
54
55         // Check if attribute "name" exists, and then getValue
56         if(rep.hasAttribute("name"))
57         {
58             myGarage.m_name = rep.getValue<std::string>("name");
59         }
60
61         // You can directly try to get the value. this function
62         // return false if there is no attribute "state"
63         if(!rep.getValue("state", myGarage.m_state))
64         {
65             std::cout << "Attribute state doesn't exist in the representation\n";
66         }
67
68
69         OCRepresentation rep2 = rep;
70
71         std::cout << "Number of attributes in rep2: "
72                   << rep2.numberOfAttributes() << std::endl;
73
74         if(rep2.erase("name"))
75         {
76             std::cout << "attribute: name, was removed successfully from rep2.\n";
77         }
78
79         std::cout << "Number of attributes in rep2: "
80                   << rep2.numberOfAttributes() << std::endl;
81
82
83         rep.getValue("light", myGarage.m_lightRep);
84
85         myGarage.m_lightRep.getValue("states", myGarage.m_lightStates);
86         myGarage.m_lightRep.getValue("powers", myGarage.m_lightPowers);
87
88         std::cout << "\tstate: " << myGarage.m_state << std::endl;
89         std::cout << "\tname: " << myGarage.m_name << std::endl;
90         std::cout << "\tlightRep: states: ";
91
92         int first = 1;
93         for(auto state: myGarage.m_lightStates)
94         {
95             if(first)
96             {
97                 std::cout << state;
98                 first = 0;
99             }
100             else
101             {
102                 std::cout << "," << state;
103             }
104         }
105
106         std::cout << std::endl;
107         std::cout << "\tlightRep: powers: ";
108         first = 1;
109         for(auto power: myGarage.m_lightPowers)
110         {
111             if(first)
112             {
113                 std::cout << power;
114                 first = 0;
115             }
116             else
117             {
118                 std::cout << "," << power;
119             }
120         }
121         std::cout << std::endl;
122
123 }
124 // callback handler on PUT request
125 void onPut(const OCRepresentation& rep, const int eCode)
126 {
127     if(eCode == SUCCESS_RESPONSE)
128     {
129         std::cout << "PUT request was successful" << std::endl;
130
131         printRepresentation(rep);
132     }
133     else
134     {
135         std::cout << "onPut Response error: " << eCode << std::endl;
136         std::exit(-1);
137     }
138 }
139
140 // Local function to put a different state for this resource
141 void putLightRepresentation(std::shared_ptr<OCResource> resource)
142 {
143     if(resource)
144     {
145         OCRepresentation rep;
146
147         std::cout << "Putting light representation..."<<std::endl;
148
149         myGarage.m_state = true;
150
151         rep.setValue("state", myGarage.m_state);
152
153         // Create QueryParameters Map and add query params (if any)
154         QueryParamsMap queryParamsMap;
155
156         // Invoke resource's pit API with rep, query map and the callback parameter
157         resource->put(rep, queryParamsMap, &onPut);
158     }
159 }
160
161 // Callback handler on GET request
162 void onGet(const OCRepresentation& rep, const int eCode)
163 {
164     if(eCode == SUCCESS_RESPONSE)
165     {
166         std::cout << "GET request was successful" << std::endl;
167         std::cout << "Resource URI: " << rep.getUri() << std::endl;
168
169         printRepresentation(rep);
170
171         putLightRepresentation(curResource);
172     }
173     else
174     {
175         std::cout << "onGET Response error: " << eCode << std::endl;
176         std::exit(-1);
177     }
178 }
179
180 // Local function to get representation of light resource
181 void getLightRepresentation(std::shared_ptr<OCResource> resource)
182 {
183     if(resource)
184     {
185         std::cout << "Getting Light Representation..."<<std::endl;
186         // Invoke resource's get API with the callback parameter
187
188         QueryParamsMap test;
189         resource->get(test, &onGet);
190     }
191 }
192
193 // Callback to found resources
194 void foundResource(std::shared_ptr<OCResource> resource)
195 {
196     if(curResource)
197     {
198         std::cout << "Found another resource, ignoring"<<std::endl;
199     }
200
201     std::string resourceURI;
202     std::string hostAddress;
203     try
204     {
205         // Do some operations with resource object.
206         if(resource)
207         {
208             std::cout<<"DISCOVERED Resource:"<<std::endl;
209             // Get the resource URI
210             resourceURI = resource->uri();
211             std::cout << "\tURI of the resource: " << resourceURI << std::endl;
212
213             // Get the resource host address
214             hostAddress = resource->host();
215             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
216
217             // Get the resource types
218             std::cout << "\tList of resource types: " << std::endl;
219             for(auto &resourceTypes : resource->getResourceTypes())
220             {
221                 std::cout << "\t\t" << resourceTypes << std::endl;
222             }
223
224             // Get the resource interfaces
225             std::cout << "\tList of resource interfaces: " << std::endl;
226             for(auto &resourceInterfaces : resource->getResourceInterfaces())
227             {
228                 std::cout << "\t\t" << resourceInterfaces << std::endl;
229             }
230
231             if(resourceURI == "/a/garage")
232             {
233                 curResource = resource;
234                 // Call a local function which will internally invoke
235                 // get API on the resource pointer
236                 getLightRepresentation(resource);
237             }
238         }
239         else
240         {
241             // Resource is invalid
242             std::cout << "Resource is invalid" << std::endl;
243         }
244
245     }
246     catch(std::exception& e)
247     {
248         //log(e.what());
249     }
250 }
251
252 int main(int argc, char* argv[]) {
253
254     // Create PlatformConfig object
255     PlatformConfig cfg {
256         OC::ServiceType::InProc,
257         OC::ModeType::Client,
258         "0.0.0.0",
259         0,
260         OC::QualityOfService::NonConfirmable
261     };
262
263     // Create a OCPlatform instance.
264     // Note: Platform creation is synchronous call.
265
266     try
267     {
268         OCPlatform platform(cfg);
269         std::cout << "Created Platform..."<<std::endl;
270         // Find all resources
271         platform.findResource("", "coap://224.0.1.187/oc/core?rt=core.garage", &foundResource);
272         std::cout<< "Finding Resource... " <<std::endl;
273         while(true)
274         {
275             // some operations
276         }
277
278     }
279     catch(OCException& e)
280     {
281         //log(e.what());
282     }
283
284     return 0;
285 }
286